Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 对存储在数组中的HTML使用jQuery选择器_Javascript_Jquery_Html_Arrays - Fatal编程技术网

Javascript 对存储在数组中的HTML使用jQuery选择器

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>" ];

我有一个数组,其中包含一些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>"
          ];
将返回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-*作为前缀