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

Js on mousehover - find matching text from two divs

我有一个代码可以在两个不同的 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.
       });

非常感谢任何想法!

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

您可以使用 jquery 的选择器来查找所有相关元素,在这种情况下,我们匹配所有在当前鼠标悬停时具有相同 class 的人。

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

您可以使用相同的想法来匹配文本,classes 等

我不建议您匹配相同的 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>