简体   繁体   English

当我有多个时,如何在这种情况下用 JS 替换 jQuery #

[英]How can I replace jQuery with JS in this situation when I have multiple #

How can I recode my jQuery, using only pure Javascript, withe multiple ids?如何仅使用具有多个 ID 的纯 Javascript 重新编码我的 jQuery?

$('#perso1').mouseenter(function() {
     $('.perso1').show(); 
     $('.name1').show(); 
     $('.perso2').hide(); 
     $('.name2').hide(); 
});

$('#perso1').mouseleave(function() {
     $('.perso2').hide(); 
     $('.name1').hide(); 
     $('.perso1').show(); 
     $('.name1').show(); 
});

$('#perso2').mouseenter(function() {
     $('.perso2').show(); 
     $('.name2').show(); 
     $('.perso1').hide(); 
     $('.name1').hide(); 
});

$('#perso2').mouseleave(function() {
     $('.perso2').hide(); 
     $('.name2').hide(); 
     $('.perso1').show(); 
     $('.name1').show(); 
});

My JS version but is not very clean and I want to target all the id at once not do the same thing over and over for every ID I have 6perso我的 JS 版本不是很干净,我想一次针对所有 id,而不是对我有 6perso 的每个 id 一遍又一遍地做同样的事情

document.getElementById("perso2,").onmouseenter = function() {mouseEnter()};
document.getElementById("perso2").onmouseleave = function() {mouseLeave()};

function mouseEnter() {
        document.getElementByClassName("perso1").style.display = 'none';
        document.getElementByClassName("name1").style.display = 'none';
        document.getElementByClassName("perso2-2").style.display = 'block';
        document.getElementByClassName("name2").style.display = 'block';
}
      
function mouseLeave() {
        document.getElementByClassName("perso1").style.display = 'block';
        document.getElementByClassName("perso2").style.display = 'none';
        document.getElementByClassName("name1").style.display = 'block';
        document.getElementByClassName("name2").style.display = 'none';
}

You can use Element.querySelector and Element.querySelectorAll which allows you您可以使用Element.querySelectorElement.querySelectorAll这允许你
to target elements that matches the specified selector as you do already with JQuery.定位与指定选择器匹配的元素,就像您已经使用 JQuery 所做的那样。

To simplify your JS code you can make some utility functions to short it.为了简化你的 JS 代码,你可以制作一些实用函数来缩短它。

Btw, your JS code is wrong.顺便说一句,你的 JS 代码是错误的。 You are making the common mistake with document.getElementByClassName .您犯了document.getElementByClassName的常见错误。
It returns a list of element like an array, so you can't access it the way you did.它像数组一样返回一个元素list ,因此您不能像以前那样访问它。

Your JQuery code您的 JQuery 代码

$('#perso1').mouseenter(function(){
     $('.perso1').show();
     $('.name1').show();
     $('.perso2').hide();
     $('.name2').hide();
});

JS version JS版本

const query = selector => document.querySelector(selector)

query('#perso1').addEventListener('mouseenter', event => {
  // Here you could use a class to toggle visibility
  query('.perso1').classList.remove('hidden')
  query('.name1').classList.remove('hidden')
  query('.perso2').classList.add('hidden')
  query('.name2').classList.add('hidden')
})

You can make as many helpers as you think you would need to make the code easier to write.您可以创建您认为需要的尽可能多的助手,以使代码更易于编写。

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

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