2017-04-12 40 views
0

我想從像這樣的屬性得到一個字符串獲取字符串...jQuery的:從屬性

$("*").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

你試過'.toString()'函數嗎? – Twisty

回答

0

console.log(attrib);讓我像name="top",這是 正是我需要的

如果字符串name="top"預期的結果,您可以連接屬性.name.value性質,將=性格上的.value兩側property

anchorLink = `${attrib.name}="${attrib.value}"`; 

anchorLink = attrib.name + '="' + attrib.value + '"'; 
0

您可以使用:JSON.stringify(attrib);