javascript - 从 DOM 访问元素
问题描述
我使用 jquery 动态创建元素。这是我的代码
function showArticles(res) {
var output = '';
res.articles.forEach(function (item) {
output +=
'<div class="col-12 col-lg-4 item-article" data-name="'+ item.author +'>'+
'<p>' + item.description + '</p>'
+
'</div>'
});
articleWrapper.append(output);
我想访问该data-name
属性
var itemArticle = $('.item-article').attr("data-type");
console.log(itemArticle)
返回undefined
。
如何访问data-name
属性为什么会发生这种情况?
解决方案
您可以使用var itemArticle = $('.item-article').data("name");
推荐阅读
- android - 一种将整数转换为仅具有字母 1...9 和 a...z 的类型的算法
- javascript - Google Forms / Apps Script / Hangouts Chat - 机器人消息内容单独发送而不是一起发送
- database - 从 MongoDB 中的两个集合获取信息
- google-bigquery - 有没有办法在 bigquery 中使用动态数据集名称
- python - 如何从数据框中加载特征和标签
- testing - 如何将输入值与结果中的值进行比较
- webpack - 是否可以在 webpack 的另一个模块中加载生成的包的内容?
- java - 在 IDE 中抛出 IllegalArgumentException 但不打印出来
- javascript - 如何将对象附加到 JQuery 中动态添加的元素
- javascript - 比较两个纵横比