简体   繁体   English

将JavaScript代码重构为jQuery代码

[英]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. 但是,由于我正在与团队合作,因此我想通过翻译此JQuery代码以提高一致性来改进它。 Can I somehow convert the code to JQuery? 我可以以某种方式将代码转换为JQuery吗?

Javascript Code JavaScript代码

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: 如果您确实要将JavaScript转换为jQuery,请执行以下操作:

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: 只要确保您正确包含了jQuery:

<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)
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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