简体   繁体   English

JavaScript:“ onmouseenter”-功能

[英]JavaScript: “onmouseenter”-Function

I'm learning some HTML, CSS, and JavaScript on my own at the moment. 目前,我正在自己学习一些HTML,CSS和JavaScript。 I have some trouble understanding the use of a function. 我在理解函数的使用时遇到了一些麻烦。

I have a div style="display: none" inside of another div. 我在另一个div内有一个div style =“ display:none”。 The "style.display" of the first div is set to "block" when the user enters the first div with his mouse. 当用户用鼠标进入第一个div时,第一个div的“ style.display”设置为“ block”。 When he leaves it the "style.display" of the second div is set to "none" again. 当他离开它时,第二个div的“ style.display”再次设置为“ none”。 Now the problem is that one function should do this to all of the divs of this kind. 现在的问题是,一个函数应该对所有此类div执行此操作。 The HTML looks like that: HTML看起来像这样:

<table>
<tr>
  <td>
    <div class="box box2" onmouseenter="show(this)" onmouseleave="hide()">
      <b>HTML</b>

      <div class="expl expl2" style="display:none" id="test">
        <ul>
          <li><b>Software</b></li>
          <li><i>HTML</i></li>
          <li>HTML, CSS, JavaScript...</li>
        </ul>
      </div>

    </div>
  </td>
  <td>
    <div class="box box2" onmouseenter="show(this)" onmouseleave="hide2()">
      <b>HTML</b>

      <div class="expl expl2" style="display:none" id="test2">
        <ul>
          <li><b>Software</b></li>
          <li><i>CSS</i></li>
          <li>HTML, CSS, JavaScript...</li>
        </ul>
      </div>

    </div>
  </td>
</tr>
</table>

The JavaScript looks like that at the moment: JavaScript现在看起来像这样:

function show(x){

  x = document.querySelector('.expl').style.display ="block";

}

I have no idea of how to correctly use "this", but I tried it here, because it seemed to be right to me, but maybe I am completely wrong. 我不知道如何正确使用“ this”,但我在这里尝试过,因为它似乎对我来说是正确的,但也许我是完全错误的。 I want to be able to add as many divs to the table as I want, but without changing the function. 我希望能够向表中添加尽可能多的div,但无需更改功能。 At the moment the only the div inside of the very first div gets the correct "style.display" if I "mouseenter" on EITHER of the outer divs. 目前,如果我在外部div的EITHER上“ mouseenter”,则第一个div内的唯一div会获得正确的“ style.display”。

Is that idea going into the right direction or is my approach wrong? 这个想法是朝正确的方向发展还是我的方法错误?

this in your code refers to the actual element from where the click originated. 您的代码中的this指的是点击产生的实际元素。 So it refers to the .box elements. 因此它指的是.box元素。

Since the .expl is a descendant of the .box element you can search deeper inside it, through the x , and find the relevant .expl element. 由于.expl.box元素的后代,因此您可以在内部通过x更深入的搜索,然后找到相关的.expl元素。

So 所以

function show(x){ // x refers to the .box element that was clicked
  x.querySelector('.expl').style.display ="block";
}

Working example 工作实例

 function show(x) { // x refers to the .box element that was clicked x.querySelector('.expl').style.display = "block"; } function hide(x) { // x refers to the .box element that was clicked x.querySelector('.expl').style.display = "none"; } 
 <table> <tr> <td> <div class="box box2" onmouseenter="show(this)" onmouseleave="hide(this)"> <b>HTML</b> <div class="expl expl2" style="display:none" id="test"> <ul> <li><b>Software</b></li> <li><i>HTML</i></li> <li>HTML, CSS, JavaScript...</li> </ul> </div> </div> </td> <td> <div class="box box2" onmouseenter="show(this)" onmouseleave="hide(this)"> <b>HTML</b> <div class="expl expl2" style="display:none" id="test2"> <ul> <li><b>Software</b></li> <li><i>CSS</i></li> <li>HTML, CSS, JavaScript...</li> </ul> </div> </div> </td> </tr> </table> 

Below is a function that can be used on any element that you want to show or hide on mouse enter. 下面是一个函数,可用于您要在鼠标输入时显示或隐藏的任何元素。

Simply add the class: triggerMouseHere to make the div triggable by javascript when the mouse enters and leaves it 只需添加类: triggerMouseHere即可在鼠标进入并离开div时通过javascript触发div

Add the class: triggerChangeOnMouseEvent to make the div visible/hidden when the mouse enters/leaves the parent div 添加类: triggerChangeOnMouseEvent以使div在鼠标进入/离开父div时可见/隐藏div

 $(function() { $(".triggerMouseHere").on("mouseenter", function() { $(this).find(".triggerChangeOnMouseEvent").first().show(); }); $(".triggerMouseHere").on("mouseleave", function() { $(this).find(".triggerChangeOnMouseEvent").first().hide(); }); }); 
 .hidden{ display: none; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table> <tr> <td> <div class="box box2 triggerMouseHere"> <b>HTML</b> <div class="expl expl2 hidden triggerChangeOnMouseEvent" id="test"> <ul> <li><b>Software</b></li> <li><i>HTML</i></li> <li class="triggerMouseHere">HTML, CSS, JavaScript... <div class="expl expl2 hidden triggerChangeOnMouseEvent" id="test"> <ul> <li><b>Software</b></li> <li><i>HTML</i></li> <li class="triggerChangeOnMouseEvent">HTML, CSS, JavaScript... </li> </ul> </div> </li> </ul> </div> </div> </td> <td> <div class="box box2 triggerMouseHere"> <b>HTML</b> <div class="expl expl2 hidden triggerChangeOnMouseEvent" id="test2"> <ul> <li><b>Software</b></li> <li><i>CSS</i></li> <li>HTML, CSS, JavaScript...</li> </ul> </div> </div> </td> </tr> </table> 

Just a question, why don't you use css to show, hide the child elements ? 只是一个问题,为什么不使用css来显示,隐藏child elements

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

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