简体   繁体   中英

Refactoring JavaScript code to jQuery code

I've modified this answer and got it working. However, I want to improve it by translating this JQuery code for uniformity since I'm working with a team. Can I somehow convert the code to JQuery?

Javascript Code

var o = document.querySelector("#divTwo");
var gg = o.querySelector('#tempType [aria-selected="true"]').innerText;
o.querySelectorAll('[templateList="me"] .entry-footer [template-include = "true"]').forEach( (elm) => {
    var a = elm.closest( '.popup-temp-entry' ).querySelector( '.entry-header' ).innerText;
    var b = elm.closest( '.popup-temp-entry' ).querySelector( '.entry-body' ).innerText;
    console.log(a +' - '+ b + ' - ' + gg);
});

If you really want to convert that JavaScript to jQuery, just do this:

var o = $("#divTwo);
var gg = o.filter("#tempType [aria-selected='true']").text();
o.filter("[templateList='me'] .entry-footer [template-include = 'true']").each((elm) => {
    var a = elm.next(".popup-temp-entry").filter(".entry-header").val();
    var b = elm.next(".popup-temp-entry").filter(".entry-body").val();
    console.log(`${a} - ${b} - gg`);
})

Just make sure you have included jQuery correctly:

<script src="https://code.jquery.com/jquery-3.3.1.js"></script>

you could also try this one.

var o = $("#divTwo"),
    gg = o.find("#tempType [aria-selected='true']").text()

o.find("[templateList='me'] .entry-footer [template-include='true']").forEach( (elm) => {
  var foo = elm.closest( '.popup-temp-entry' ),
      a = foo.find(".entry-header").text(),
      b = foo.find("entry-body").text()
  console.log(gg, a, b)
})

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