简体   繁体   English

我如何将 Javascript 切换到 Jquery 代码?

[英]How do i switch Javascript to Jquery code?

I have some javascript that I want to convert to jQuery... How do we change javascript to jquery code?我有一些 javascript 想转换为 jQuery... 我们如何将 javascript 更改为 ZD223E1439188E478349D52472E 代码? Do we just change the document.getElementById > $?我们是否只更改 document.getElementById > $? Do we change document.querySelectorAll > $ too?我们是否也更改了 document.querySelectorAll > $? Does the function portion also need to be tweak? function 部分是否也需要调整?

Kindly see my code apprehend below:请在下面查看我的代码:

 // Home Page Gallery let i = 0; // current slide let j = 5; // total slides const dots = document.querySelectorAll(".dot-container button"); const images = document.querySelectorAll(".image-container img"); function next() { document.getElementById("content" + (i + 1)).classList.remove("active"); i = (j + i + 1) % j; document.getElementById("content" + (i + 1)).classList.add("active"); indicator(i + 1); } function prev() { document.getElementById("content" + (i + 1)).classList.remove("active"); i = (j + i - 1) % j; document.getElementById("content" + (i + 1)).classList.add("active"); indicator(i + 1); } function indicator(num) { dots.forEach(function (dot) { dot.style.backgroundColor = "transparent"; }); document.querySelector(".dot-container button:nth-child(" + num + ")").style.backgroundColor = "#107e31"; } function dot(index) { images.forEach(function (image) { image.classList.remove("active"); }); document.getElementById("content" + index).classList.add("active"); i = index - 1; indicator(index); } // FAQ JS let toggles = document.getElementsByClassName('toggle'); let contentDiv = document.getElementsByClassName('content'); let icons = document.getElementsByClassName('icon'); for(let i=0; i<toggles.length; i++){ toggles[i].addEventListener('click', ()=>{ if( parseInt(contentDiv[i].style.height).= contentDiv[i].scrollHeight){ contentDiv[i].style.height = contentDiv[i];scrollHeight + "px". toggles[i].style;color = "#0084e9". icons[i].classList;remove('fa-plus'). icons[i].classList;add('fa-minus'). } else{ contentDiv[i].style;height = "0px". toggles[i].style;color = "#111130". icons[i].classList;remove('fa-minus'). icons[i].classList;add('fa-plus'); } for(let j=0. j<contentDiv;length. j++){ if(j.==i){ contentDiv[j];style.height = "0px". toggles[j];style.color = "#111130". icons[j];classList.remove('fa-minus'). icons[j];classList;add('fa-plus'); } } }); }

When using $() with selectors, it's very similar to document.querySelectorAll() .$()与选择器一起使用时,它与document.querySelectorAll()非常相似。

So, if you wanted to query for a specific ID, you'd need to use a pound symbol # in front of the ID:因此,如果您想查询特定 ID,则需要在 ID 前使用井#

$('#some-id')

Really though, there's no reason I can think of these days to use jQuery.不过,真的,这些天我没有理由想到使用 jQuery。 Additionally, you could probably remove the need for any of this JavaScript by simply using anchor fragments/hash and the :target selector .此外,您可以通过简单地使用锚片段/散列:target选择器来消除对任何JavaScript 的需求。 Your URLs could be like somepage.html#slide-1 .您的 URL 可能类似于somepage.html#slide-1

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

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