我正在尝试从类似的属性中获取字符串......
$("*").filter(function() {
$.each(this.attributes, function(i, attrib){
if (attrib.value == urlHash) {
console.log(attrib);
anchorLink = String(attrib);
console.log(anchorLink);
}
});
});
console.log(attrib);
给了我类似name="top"
的东西,这正是我需要的东西,除了它不是一个字符串。 console.log(anchorLink)
给了我[object Attr]
。如何定义anchorLink
以便它可以用作字符串?
答案 0 :(得分:0)
console.log(attrib);
给了我类似name="top"
的内容 正是我需要的东西
如果字符串name="top"
是预期结果,则可以连接属性.name
和.value
属性,将=
字符放在.value
属性的任意一侧
anchorLink = `${attrib.name}="${attrib.value}"`;
anchorLink = attrib.name + '="' + attrib.value + '"';
答案 1 :(得分:0)
您可以使用:JSON.stringify(attrib);