获得徽章 0
##attr和prop区别
attr(attribute的缩写) 是从页面搜索获得元素值,所以页面必须明确定义元素才能获取值。
prop(property的缩写)是从属性对象中取值,属性对象中有多少属性,就能获取多少值,不需要在页面中显示定义。
—————
对于HTML元素本身就带有的固有属性,使用prop方法;在处理我们自己自定义的DOM属性时,则使用attr方法。
—————
<div abc="123"></div>

// 由于 abc并非 HTML标准属性,因此 $("div").prop("abc") 无法获取到任何值。
console.log($("div").prop("abc")); //undefined
// 但 attr 不受限制。$("div").attr("abc") 会扫描源码并获取到 "123" 的结果。
console.log($("div").attr("abc")); //123
展开
1
使用模板字面量(允许嵌入表达式的字符串字面量):
let html = "";
for (let index = 0; index < data.length; index++) {
const element = data[index];
html +=
`<div class="alert alert-info pull-left">
<button type="button" data-id="${element._id}" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
<strong>${element.text}</strong>
</div>`;
}
展开
评论
关注了标签 HTML HTML
下一页
个人成就
文章被点赞 17
文章被阅读 11,216
掘力值 318
收藏集
0
关注标签
8
加入于