87,903
社区成员
发帖
与我相关
我的任务
分享
<title testAA="tt"></title>
<h1 testAA="hh"></h1>
<p testAA="pp">采购计划</p>
//获取页面所有 自定义属性 testAA的元素, 那么jq的是这么写的,
$("[testAA]").each(function() {
});
//原生js 如何写?
document.querySelectorAll("[testAA]").forEach(x => console.log(x));
<title testaa="tt"></title>
<h1 testaa="hh"></h1>
<p testaa="pp">采购计划</p>
<script>
var els = [];
if (document.querySelectorAll) els = document.querySelectorAll('[testaa]')
else {
var all = document.getElementsByTagName('*');
//ie8-的title自定义属性会被去掉,获取不到title
for (var i = 0; i < all.length; i++) if (all[i].getAttribute('testaa') !== null) els.push(all[i]);
}
console.log(els)
</script>