简体   繁体   English

如何找到与 jquery 代码等效的 javascript?

[英]How to find javascript equivalent of jquery code?

I've got a problem on a WordPress site.我在 WordPress 网站上遇到了问题。 A plugin is going to crash when I include jQuery on the site.当我在网站上包含 jQuery 时,插件会崩溃。 That's why I'd like to include simple javascript code.这就是为什么我想包含简单的 javascript 代码。 However the problem is that I don't know its Javascript equivalent.然而问题是我不知道它的 Javascript 等价物。

$(document).ready(function() {
  $('#sd').change(function() {
    var n = new Date(this.value);
    n.setDate(n.getDate() + 1);

    var day = ("0" + n.getDate()).slice(-2);
    var month = ("0" + (n.getMonth() + 1)).slice(-2);
    var today = n.getFullYear() + "-" + (month) + "-" + (day);
    $('#ed').attr('min', today);
  });
});

the error log (wordpress):错误日志(wordpress):

Uncaught TypeError: r.getClientRects is not a function
    at w.fn.init.offset (jquery-3.3.1.min.js:2)
    at Object.getWithinInfo (position.min.js?ver=1.11.4:11)
    at w.fn.init.a.fn.position (position.min.js?ver=1.11.4:11)
    at w.fn.init.reposition (pum-site-scripts.js?…263704&ver=1.7.29:7)
    at w.fn.init.e.fn.popmake (pum-site-scripts.js?…263704&ver=1.7.29:7)
    at w.fn.init.open (pum-site-scripts.js?…263704&ver=1.7.29:7)
    at w.fn.init.e.fn.popmake (pum-site-scripts.js?…263704&ver=1.7.29:7)
    at HTMLAnchorElement.<anonymous> (pum-site-scripts.js?…263704&ver=1.7.29:8)
    at HTMLDocument.dispatch (jquery-3.3.1.min.js:2)
    at HTMLDocument.y.handle (jquery-3.3.1.min.js:2)

The JS equivalent to $(selector) is document.querySelector(selector) or document.querySelectorAll(selector) , depending on whether you want just the first match or all the matches.相当于$(selector)的 JS 是document.querySelector(selector)document.querySelectorAll(selector) ,这取决于您是想要第一个匹配项还是所有匹配项。 In your code, since you're selecting an ID, you only need the first match.在您的代码中,由于您选择的是 ID,因此您只需要第一个匹配项。

The plain JS method for adding an event handler is .addEventListener .添加事件处理程序的普通 JS 方法是.addEventListener

The equivalent to .attr() is .setAttribute() . .attr()的等价物是.setAttribute()

The rest of the code is plain JS, not jQuery.其余代码是纯 JS,而不是 jQuery。

 window.addEventListener("DOMContentReady", function() { document.querySelector("#sd").addEventListener("change", function() { var n = new Date(this.value); n.setDate(n.getDate() + 1); var day = ("0" + n.getDate()).slice(-2); var month = ("0" + (n.getMonth() + 1)).slice(-2); var today = n.getFullYear() + "-" + (month) + "-" + (day); document.querySelector("#ed").setAttribute('min', today); }); });

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

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