Javascript 对存储在数组中的HTML使用jQuery选择器
我有一个数组,其中包含一些HTML,因此类似于:Javascript 对存储在数组中的HTML使用jQuery选择器,javascript,jquery,html,arrays,Javascript,Jquery,Html,Arrays,我有一个数组,其中包含一些HTML,因此类似于: var array=[ "<div anAttribute=19 class='foo'>This is the content of the foo div.</div>", "<div anAttribute=14 class='bar'>This is the content of the bar div.</div>" ];
var array=[
"<div anAttribute=19 class='foo'>This is the content of the foo div.</div>",
"<div anAttribute=14 class='bar'>This is the content of the bar div.</div>"
];
将返回19。您可以这样做:
$(array[0]).attr("anAttribute");
$(array[1]).attr("anAttribute");
我假设anAttribute
是一个有效的html属性,如果不在它前面加上data-*
前缀,您可以这样做:
$(array[0]).attr("anAttribute");
$(array[1]).attr("anAttribute");
我假设
anAttribute
是一个有效的html属性,如果不在它前面加上data-*
前缀,则会在数组中显示属性:
var attributes = $(array.join('')).map(function(){
return $(this).attr('anAtribute');
}).get();
然后,您可以通过以下方式访问它们:
attributes[0] //= 19
attributes[1] //= 14
这将为您提供数组中的属性:
var attributes = $(array.join('')).map(function(){
return $(this).attr('anAtribute');
}).get();
然后,您可以通过以下方式访问它们:
attributes[0] //= 19
attributes[1] //= 14
您只需要将字符串包装到jquery闭包中:
jQuery(array[0]).attr('anAttribute');
将返回19您只需将字符串包装在jquery闭包中:
jQuery(array[0]).attr('anAttribute');
将返回19您可以像在HTML5中那样创建自定义属性。谢谢你的回答,我会尽快接受的@ebol4是的自定义属性必须以数据为前缀-*在HTML 5中,您可以像在HTML5中那样组成自定义属性。谢谢你的回答,我会尽快接受的@ebol4 yes自定义属性在HTML 5中必须以data-*作为前缀