使用jquery从html中提取属性
问题描述:
我有一个从outerHTML获得的字符串。它记录,如:使用jquery从html中提取属性
var responseHtml = jQuery(data).find("#response")[0].outerHTML;
console.log(responseHtml);
Log:
<div id="response" hello="world" big="span" hey="there"><\/div>"
<div class="clear"></div></div>
我想获得它的格式,所以我可以进入“世界”,如:values["hello"] => "world"
我在这里停留。什么是实现它的适当方式?
答
而不是创建无效的属性,你应该使用data
属性,如:
<div id="response" data-hello="world" data-big="span" data-hey="there"></div>
然后让使用jQuery这些值,你可以这样做:
var response = $('#response');
response.data('hello'); // returns "world"
response.data('big'); // returns "span"
response.data('hey'); // returns "there"
首先,所有这些属性都是无效的,你应该使用数据属性。其次,你应该简单地将它们作为$('#response')。data('big')'来访问它们。 – adeneo
你可以用'$('#response')。attr('hello')' –
非常感谢!!! – senty