如何将此jQuery代码转换为Vanilla JS?

时间:2020-05-11 15:04:58

标签: javascript jquery

如何转换此jQuery代码

$(element).find('.ui.dropdown') 

使用纯JS?

我需要将JQUERY中的简单代码转换为纯JS。

element是我的代码中的一个变量,它来自以下代码:

$.each($("[action]").toArray(), function(key, element)

1 个答案:

答案 0 :(得分:1)

以下内容如何?

Array.prototype.slice.call(document.querySelectorAll('[action]')).forEach(function (element, index) {
  element.querySelector('.ui.dropdown')
  // do something to the element
});

如前所述,仅当您运行的浏览器不支持Array.prototype.slice()时才需要NodeList.prototype.forEach()。对于现代浏览器,您实际上可以执行以下操作:

document.querySelectorAll('[action]').forEach(function (element, index) {
   element.querySelector('.ui.dropdown')
  // do something to the element
});