jQuery:从属性中获取字符串

时间:2017-04-12 00:20:48

标签: javascript jquery string

我正在尝试从类似的属性中获取字符串......

$("*").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以便它可以用作字符串?

2 个答案:

答案 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);