我想将span
节点的visited
属性设置为true
或false
基于它是否已被访问。
test();
function test () {
var el = document.createElement("span");
el.setAttribute("visited", false);
el.setAttribute("visited", true);
alert(el.getAttribute("visited") === true); //False
alert(el.getAttribute("visited") === "true"); //True
}
我最初将属性“已访问”设置为 boolean false,然后将 boolean 设置为true。我注意到,当我检查属性是否为 时true
,它返回 false,但如果我检查字符串true
,它返回 true。
MSN Docs只讨论 attributeName 需要是字符串,而不是值。那么为什么不与布尔值进行比较呢?