在JavaScript中使用Mouseeter/MouseLeave更改Div

Using Mouseenter / MouseLeave to Change Div in JavaScript

本文关键字:MouseLeave 更改 Div Mouseeter JavaScript      更新时间:2023-09-26

当mouseenter和mouseleave函数被触发时,我正试图使用数组索引来允许一组div ID从一个ID更改为另一个ID。

我知道还有其他方法可以做到这一点——切换、悬停或CSS悬停。这对我来说只是一种学习,而且我还是个新手。

下面的代码是有注释的,基本问题与为什么"largeID"(或smallID)的数组变量输出正确的值有关,而尝试使用索引值却没有。对于每个For语句,当鼠标进入div时,我希望用largeID[I]值替换smallID[I]的值,但我不想为每个语句编写代码,即"largeID[1],largeID[2]。

谢谢你的指点!!

$(document).ready(function() {
    var smallID = [];
    var largeID = [];
    var divList = document.getElementsByTagName('div')[1]; //get the second (radialMenu) div in the document
    var radialDivList = divList.getElementsByTagName('div'); // get all divs under the second (radialMenu) div
    for(var i = 0; i < radialDivList.length; i++) {
        if (!radialDivList[i]) continue; //skip null, undefined and non-existent elements
        if (!radialDivList.hasOwnProperty(i)) continue; //skip inherited properties
        smallID[i] = radialDivList[i].id; //assign the list of four divs to the smallID array;
        largeID[i] = smallID[i] + 'Full'; // add "Full" to each smallID element to make a largeID element
        alert(smallID[i]); // alerts for smallID / largeID and smallID[i] / largeID[i]
        alert(largeID[i]); // give rational and expected output
        $('#' + smallID[i]).mouseenter(function () { //works for all four radial menu divs when mouse enters
            //BUT largeID[i] is undefined
            alert(largeID[i]); // undefined
            alert(largeID); // gives expected output of full array
        }).mouseleave(function () { //mouseleave function not working
        });
    }

您的largeID[i]在mouseenter函数中未定义的原因是,i的最后一个值被记住并在mousecenter事件中使用。

当您使用一个变量并且它"超出范围"时,会自动创建一个闭包,以允许仍然需要它的函数仍然存在该变量,并且您的mouseenter函数都引用同一个变量。

当i超过您使用radialDivList.length的div数量时,您的for循环将停止。现在,每次尝试使用i引用数组中的索引都将越界。

本页的第一个答案很好地解释了这一点:循环中的JavaScript闭包——简单实用的示例

我修改了你的例子,用它自己的"I"副本创建了一个新函数。所以当悬停在第一个div上时,i将等于0,当悬停在第二个div上,它将等于1,等等。

$(document).ready(function() {
  var smallID = [];
  var largeID = [];
  var divList = document.getElementsByTagName('div')[1]; //get the second (radialMenu) div in the document
  var radialDivList = divList.getElementsByTagName('div'); // get all divs under the second (radialMenu) div
  var funcs = [];
  for (var i = 0; i < radialDivList.length; i++) {
    if (!radialDivList[i]) continue; //skip null, undefined and non-existent elements
    if (!radialDivList.hasOwnProperty(i)) continue; //skip inherited properties
    smallID[i] = radialDivList[i].id; //assign the list of four divs to the smallID array;
    largeID[i] = smallID[i] + 'Full'; // add "Full" to each smallID element to make a largeID element
    alert(smallID[i]); // alerts for smallID / largeID and smallID[i] / largeID[i]
    alert(largeID[i]); // give rational and expected output
    funcs[i] = function(i) {
      $('#' + smallID[i]).mouseenter(function() { //works for all four radial menu divs when mouse enters
        //BUT largeID[i] is undefined
        alert(largeID[i]); // undefined
        alert(largeID); // gives expected output of full array
      }).mouseleave(function() { //mouseleave function not working
      });
    }.bind(this, i);
  }
  for (var i = 0; i < funcs.length; i++) {
    funcs[i]();
  }
});
<html>
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  <title>Example</title>
</head>
<body>
  <div>
    <div>
      <div id="one" style="background:green;height:40px">
      </div>
      <div id="two" style="background:red;height:40px">
      </div>
      <div id="three" style="background:blue;height:40px">
      </div>
    </div>
  </div>
</body>
</html>