我使用 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)
返回未定义
。
如何访问 data-name
属性为什么会这样?
最佳答案
您可以使用 var itemArticle = $('.item-article').data("name");
关于javascript - 从 DOM 访问元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56080017/