作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
allCookies 内容是我的浏览器 Cookie 列表。我想用 delCookie() 这个函数删除 Cookie,但它只删除第一个 Cookie,而不删除其他 Cookie。我该如何更新 cookie???
<input type="button" value="DElete" onclick="delCookie()">
<input type="button" value="Update" onclick="modCookie()">
<select multiple id="allCookies" size="5">
<!-- Cookies content-->
</select><br>
function delCookie() {
if (confirm('Are u sure?')) {
var e = document.getElementById("allCookies");
var strUser = e.options[e.selectedIndex].value;
document.cookie = encodeURIComponent(strUser) + "=deleted; expires=" + new Date(0).toUTCString();
}
}
最佳答案
It just delete the first Cookie, and not the others cookies.
selectedIndex
<select>
的属性元素仅返回第一个选定选项的索引。要检查 multiple
中的所有内容选择,您将需要迭代选项集合:
var os = document.getElementById("allCookies").options;
for (var i=0; i<os.length; i++) {
if (os[i].selected) {
var strUser = os[i].value;
…
}
}
And how could i do to update a cookie???
只需覆盖 cookie,即使用与创建它们时相同的方法。
关于javascript - 如何用javascript读取和删除Cookie?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14811421/
我是一名优秀的程序员,十分优秀!