gpt4 book ai didi

javascript - 将 div 的内联样式保存到 cookie 不起作用

转载 作者:行者123 更新时间:2023-11-30 14:39:53 25 4
gpt4 key购买 nike

我对这个 html 页面有疑问。cookie中应该保存box的位置,但是只保存了top:属性,没有保存top和left。 enter image description here被保存而不是

enter image description here

function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays*24*60*60*1000));
var expires = "expires="+ d.toUTCString();
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}
function getCookie(cname) {
var name = cname + "=";
var decodedCookie = decodeURIComponent(document.cookie);
var ca = decodedCookie.split(';');
for(var i = 0; i <ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
//Make the DIV element draggagle:
document.getElementById("mydiv").setAttribute("style",getCookie("a"));
dragElement(document.getElementById(("mydiv")));

function dragElement(elmnt) {
var pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0;
if (document.getElementById(elmnt.id + "header")) {
/* if present, the header is where you move the DIV from:*/
document.getElementById(elmnt.id + "header").onmousedown = dragMouseDown;
} else {
/* otherwise, move the DIV from anywhere inside the DIV:*/
elmnt.onmousedown = dragMouseDown;
}

function dragMouseDown(e) {
e = e || window.event;
// get the mouse cursor position at startup:
pos3 = e.clientX;
pos4 = e.clientY;
document.onmouseup = closeDragElement;
// call a function whenever the cursor moves:
document.onmousemove = elementDrag;
}

function elementDrag(e) {
e = e || window.event;
// calculate the new cursor position:
pos1 = pos3 - e.clientX;
pos2 = pos4 - e.clientY;
pos3 = e.clientX;
pos4 = e.clientY;
// set the element's new position:
elmnt.style.top = (elmnt.offsetTop - pos2) + "px";
elmnt.style.left = (elmnt.offsetLeft - pos1) + "px";
}

function closeDragElement() {
setCookie("a",document.getElementById("mydiv").style.cssText,1);
/* stop moving when mouse button is released:*/
document.onmouseup = null;
document.onmousemove = null;
}
}
#mydiv {
position: absolute;
z-index: 9;
background-color: #f1f1f1;
text-align: center;
border: 1px solid #d3d3d3;
}

#mydivheader {
padding: 10px;
cursor: move;
z-index: 10;
background-color: #2196F3;
color: #fff;
}
<!DOCTYPE html>
<html>
<body>

<div id="mydiv" >
<div id="mydivheader">Title</div>
<div contenteditable="true">
<p>Move gfsdg gt gf gfg g fdgdfgfdg</p>
</div>
</div>
</body>
</html>

我试图在框拖动结束时将属性“style”的值放入 cookie“a”中。

我更喜欢非 jquery 答案。

谢谢。

最佳答案

Note: I agree with Nisarg's answer, using localStorage is a lot easier than cookies, so you should consider using that instead.

Cookie 名称或值中不允许使用分号。它们是有含义的,用于分隔参数。

您需要用其他东西替换它们,例如:

function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays*24*60*60*1000));
var expires = "expires="+ d.toUTCString();
// Replace semicolons before saving the cookie
var escapedCvalue = cvalue.replace(/;/g, '{{semicolon}}'); // <-------
document.cookie = cname + "=" + escapedCvalue + ";" + expires + ";path=/"; // <-------
}
function getCookie(cname) {
var name = cname + "=";
var decodedCookie = decodeURIComponent(document.cookie);
var ca = decodedCookie .split(';');
for(var i = 0; i <ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length)
// Put semicolons back in there
.replace(/\{\{semicolon\}\}/g, ';'); // <------
}
}
return "";
}

关于javascript - 将 div 的内联样式保存到 cookie 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49831907/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com