Javascript 不使用JQuery从DOM隐藏选定元素
以下代码在本机JS中是什么样子的Javascript 不使用JQuery从DOM隐藏选定元素,javascript,jquery,Javascript,Jquery,以下代码在本机JS中是什么样子的 $(".custom-popover").hide(); 这个问题有点宽泛。jQuery在内部是这样做的,然后是一种只使用本机JavaScript的方式,与jQuery的操作方式无关: [].slice.call( document.querySelectorAll('.custom-popover')).forEach(function (el) { el.style.display = 'none'; } ); 由于doc
$(".custom-popover").hide();
这个问题有点宽泛。jQuery在内部是这样做的,然后是一种只使用本机JavaScript的方式,与jQuery的操作方式无关:
[].slice.call(
document.querySelectorAll('.custom-popover')).forEach(function (el) {
el.style.display = 'none';
}
);
由于document.querySelectorAll
返回一个不能与forEach
一起使用的nodelist
,因此可以通过调用节点列表上的切片
将其转换为实际数组。之后,循环查找所有找到的内容并更新style
属性
这是一个备选方案,不使用forEach
,尽管我更喜欢上述方法:
var els = document.querySelectorAll('.custom-popover');
for (var i = 0; i < els.length; i++) {
els[i].style.display = 'none';
}
var els=document.querySelectorAll('.custom popover');
对于(变量i=0;i
这个问题有点宽泛。jQuery在内部是这样做的,然后是一种只使用本机JavaScript的方式,与jQuery的操作方式无关:
[].slice.call(
document.querySelectorAll('.custom-popover')).forEach(function (el) {
el.style.display = 'none';
}
);
由于document.querySelectorAll
返回一个不能与forEach
一起使用的nodelist
,因此可以通过调用节点列表上的切片
将其转换为实际数组。之后,循环查找所有找到的内容并更新style
属性
这是一个备选方案,不使用forEach
,尽管我更喜欢上述方法:
var els = document.querySelectorAll('.custom-popover');
for (var i = 0; i < els.length; i++) {
els[i].style.display = 'none';
}
var els=document.querySelectorAll('.custom popover');
对于(变量i=0;i
为什么我们需要[].slice.call()?@Parabhayani,因为我决定使用forEach
,它必须是一个实际的数组,forEach
不能在每个浏览器的NodeList
上工作。你能给我展示一些我可以学习的示例或链接吗this@ParagBhayani,这里还有几个链接:,当然,谢谢你的点头致谢!正是我所需要的。为什么我们需要[].slice.call()?@Parabhayani,因为我决定使用forEach
,它必须是一个实际的数组,forEach
不能在每个浏览器中对NodeList
工作。你能给我展示一些我可以学习的示例或链接吗this@ParagBhayani,这里还有几个链接:,当然,谢谢你的点头致谢!正是我需要的。