javascript - 在存储在数组中的 HTML 上使用 jQuery 选择器

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

我想获取它们两个的 anAttribute 值。

array[0].$('.foo').attr("anAttribute");

将返回 19。

最佳答案

你可以这样做:

$(array[0]).attr("anAttribute");
$(array[1]).attr("anAttribute");

我假设 anAttribute 是一个有效的 html 属性,如果没有使用 data-* 前缀的话。

关于javascript - 在存储在数组中的 HTML 上使用 jQuery 选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17439100/

相关文章:

javascript - 我如何将 javascript 变量包装在 span 或 bold 标记中?

javascript - 将 JavaScript 嵌入 XML 兼容的 JSP 文件 (JSPX)

javascript - 在 React.Component 的 ES6 类实例上调用方法

javascript - 在上一张幻灯片后继续滚动

javascript - 对基于 ColdFusion 的 Web 应用程序进行代码签名

html - EL表达式不平衡jsf

javascript - 水平和垂直对齐 DIV

javascript - 使用 ng-model 指令从 select2 中删除空选项

javascript - 保护网络应用程序免受信息泄露

JavaScript 同步自定义提示