简体   繁体   中英

How to convert this jQuery code to Vanilla JS?

How to convert this jquery code

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

to pure JS?

I need to convert a simple code in JQUERY to pure JS.

The element is a variable in my code, it comes from this code:

$.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() . For modern browsers you can actually do:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM