[英]How to convert this jQuery code to Vanilla JS?
How to convert this jquery code如何转换此 jquery 代码
$(element).find('.ui.dropdown')
to pure JS?纯JS?
I need to convert a simple code in JQUERY to pure JS.我需要将 JQUERY 中的一个简单代码转换为纯 JS。
The element
is a variable in my code, it comes from this code:该
element
是我代码中的一个变量,它来自以下代码:
$.each($("[action]").toArray(), function(key, element)
How about the following?下面的呢?
Array.prototype.slice.call(document.querySelectorAll('[action]')).forEach(function (element, index) {
element.querySelector('.ui.dropdown')
// do something to the element
});
As noted, Array.prototype.slice()
is only needed if the browser you're running doesn't support NodeList.prototype.forEach()
.如前所述,仅当您运行的浏览器不支持
NodeList.prototype.forEach()
时才需要Array.prototype.slice()
) 。 For modern browsers you can actually do:对于现代浏览器,您实际上可以这样做:
document.querySelectorAll('[action]').forEach(function (element, index) {
element.querySelector('.ui.dropdown')
// do something to the element
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.