鼠标悬停上的 Js - 从两个 div 中查找匹配的文本



我有一个代码,在两个不同的div中显示几个用户名。 这个想法是让用户找到匹配的名称或没有匹配的名称。

我的想法是制作一个类似于浏览器 F3 按钮的功能,但在鼠标悬停时找到匹配项

前任:

<div id="fccStatus" class="col-md-6">
<h2 style="text-align: center;color:white;">
Users you follow :
</h2>
<p id="a">a</p>
<p id="b">b</p>
<br>
</div>
<div id="links" class="col-md-6">
<h2 style="text-align: center;color:white;">
Users who followed back :                    
</h2>
<p id="a">a</p>
<p id="b">b</p>
<br>
</div> 
$( "#body" ).mouseover(function() {
// find matching ids || text || classes .. e.t.c.
});

任何想法都非常感谢!

您可以使用 jquery 的选择器来查找所有相关元素,在本例中,我们将匹配所有具有当前鼠标悬停相同类的人。

当它离开时,只需更改为初始颜色。

您可以使用相同的想法来匹配文本、类等。

我不建议您匹配相同的id,因为id必须是唯一的。

$("p").mouseover(function() {
// find matching ids || text || classes .. e.t.c.
$("p."+$(this).attr("class")).css("color","red");
});
$("p").mouseleave(function() {
// find matching ids || text || classes .. e.t.c.
$("p."+$(this).attr("class")).css("color","black");
});
body {
background: #20262E;
padding: 20px;
font-family: Helvetica;
}
#banner-message {
background: #fff;
border-radius: 4px;
padding: 20px;
font-size: 25px;
text-align: center;
transition: all 0.2s;
margin: 0 auto;
width: 300px;
}
button {
background: #0084ff;
border: none;
border-radius: 5px;
padding: 8px 14px;
font-size: 15px;
color: #fff;
}
#banner-message.alt {
background: #0084ff;
color: #fff;
margin-top: 40px;
width: 200px;
}
#banner-message.alt button {
background: #fff;
color: #000;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="fccStatus" class="col-md-6">
<h2 style="text-align: center;color:white;">
Users you follow :
</h2>
<p class="a">a</p>
<p class="b">b</p>
<br>
</div>
<div id="links" class="col-md-6">
<h2 style="text-align: center;color:white;">
Users who followed back :                    
</h2>
<p class="a">a</p>
<p class="b">b</p>
<br>
</div>

<div id="fccStatus" class="col-md-6">
<h2 style="text-align: center;color:white;">
Users you follow :
</h2>
<p id="a">a</p>
<p id="b">b</p>
<br>
</div>
<div id="links" class="col-md-6">
<h2 style="text-align: center;color:white;">
Users who followed back :                    
</h2>
<p id="a">a</p>
<p id="b">b</p>
<br>
</div> 
$( "#body" ).mouseover(function() {
// find matching ids || text || classes .. e.t.c.
});


let divs = document.querySelectorAll('.col-md-6');
divs.forEach(el=>{
el.addEventListener('mouseenter',(){
//somefunction
});


})

最新更新