addEventListener 单击有效但鼠标悬停无效

addEventListener click works but not mouseover

我知道我在做一些愚蠢的事情,但对于我来说,我看不出它是什么。

我的名字有 div 个字母放在一个容器中。这个想法是 'mouseover' 字母并更改字符的大小,尽管鼠标悬停不起作用。如果我将事件更改为 'click' 那么它确实有效。

为什么 'mouseover' 不能工作而 'click' 可以?

我是否使用了错误的事件将鼠标悬停在元素上?

document.getElementById('profile_name').addEventListener('mouseover', function(e) {
  e.target.style.fontSize = '10px';

}, false);
<div class="profile_name_container">
  <div id="profile_name">
    <span class="letter">J</span>
    <span class="letter">e</span>
    <span class="letter">s</span>
    <span class="letter">s</span>
    <span class="letter">i</span>
    <span class="letter">c</span>
    <span class="letter">a</span>
    <span class="letter">.</span>
    <span class="letter">R</span>
    <span class="letter">y</span>
    <span class="letter">a</span>
    <span class="letter">n</span>
  </div>
</div>

试试这个。

   
var item = document.getElementById("profile_name");
item.addEventListener("mouseover", func, false);
item.addEventListener("mouseout", func1, false);

function func(e)
{  
   e.target.style.fontSize = '10px';
   e.target.style.color = 'red';
   
}

function func1(e)
{  
   e.target.style.fontSize = '16px';
   e.target.style.color = '#000';
}
<div class="profile_name_container">
     <div id="profile_name">
          <span class="letter">J</span>
          <span class="letter">e</span>
          <span class="letter">s</span>
          <span class="letter">s</span>
          <span class="letter">i</span>
          <span class="letter">c</span>
          <span class="letter">a</span>
          <span class="letter">.</span>
          <span class="letter">R</span>
          <span class="letter">y</span>
          <span class="letter">a</span>
          <span class="letter">n</span>
     </div>
</div>

  

我试图在开发人员控制台打开时对此进行测试,这阻止了读取鼠标悬停事件。

我还调整了我的 js,以便它使用事件委托针对单个字母:


    document.getElementById('profile_name').addEventListener('mouseover', function(e) {
       if (e.target.className === 'letter') {
           e.target.style.fontSize = '10px';
       }
           
    }, false);

现在正在运行。

您可以将事件附加到每个 .letter

但更好的方法是使用事件冒泡并检查目标是否具有 class letter.

这种技术称为事件委托。另一个好处是,如果以后你添加更多的字母,你不需要再添加任何事件。

例如..

document.getElementById('profile_name').addEventListener('mouseover', function(e) {
  if (e.target.classList.contains('letter'))
    e.target.style.fontSize = '10px';
}, false);
<div class="profile_name_container">
  <div id="profile_name">
    <span class="letter">J</span>
    <span class="letter">e</span>
    <span class="letter">s</span>
    <span class="letter">s</span>
    <span class="letter">i</span>
    <span class="letter">c</span>
    <span class="letter">a</span>
    <span class="letter">.</span>
    <span class="letter">R</span>
    <span class="letter">y</span>
    <span class="letter">a</span>
    <span class="letter">n</span>
  </div>
</div>

您将 mouseover 事件附加到 ID 为 profile_name 且包含所有字符的元素。您正在使用元素的 target 属性,该元素将成为事件附加到的元素。在这种情况下,它是包含元素。因此,包含元素的文本大小是正在更改的样式,而不是字符所在的元素。

以下应该有效

var a = document.getElementById('profile_name').getElementsByClassName('letter');

for (var i = 0; i < a.length; i++) {

    (function(elem) {
        elem.addEventListener('mouseover', function(e) {
            e.target.style.fontSize = '10px';
        })
    })(a[i]);
}