简体   繁体   English

使用JS函数在div中删除mouseover

[英]Remove mouseover in div with JS-Function

I want to be able to enable and disable the mouseover function call of a certain div-id and it just won't work. 我希望能够启用和禁用某个div-id的mouseover函数调用,但这将无法正常工作。

function revertcontent(){
  document.getElementById('content').src='img/content.jpg';
  document.getElementById('zentrum').addEventlistener("mouseover",true);
}
function dias( klick ){
  document.getElementById('content').src='img/'+klick+'.jpg';
  document.getElementById('zentrum').removeEventlistener("mouseover",true);
}

here is the div, that has the mouseover function call: 这是div,具有mouseover函数调用:

<div class="cont">
    <div id="zentrum" onmouseover="showvita()" onmouseout="revertcontent()">
         <img src="img/content.jpg" id="content" />
    </div>
</div>

So, I basically want to disable the mouseover when the function (dias) is called, and reenable it when the function (revertcontent) is called. 因此,我基本上想在调用函数(dias)时禁用鼠标悬停,并在调用函数(revertcontent)时重新启用鼠标悬停。 (revertcontent does get called by other button presses as well. (revertcontent确实也会被其他按钮按下来调用。

It doesn't seem to work at all with the EventListeners. 它与EventListeners似乎根本不起作用。

Thanks in advance for your help! 在此先感谢您的帮助! I am no coder, just a hobby-ist 我不是编码员,而是业余爱好者

Why not use jQuery which makes it more easier. 为什么不使用jQuery ,它使它变得更容易。 Make your html look like this 使您的html看起来像这样

<div class="cont"><div id="zentrum">
    <img src="img/content.jpg" id="content" />
</div></div>

Then use the jQuery hover() function like this 然后像这样使用jQuery hover()函数

$('div#zentrum').hover(function(){
      $('img#content').attr('src', 'img/content.jpg');
},
function(){
    // run all your code for the mouseleave here
});

there just isn't enough space to comment easily. 只是没有足够的空间来轻松评论。

I changed the code as follows: 我将代码更改如下:

  function showvita(){ document.getElementById('content').src='img/vita.jpg'; document.getElementById('zentrum').onmouseout = "revertcontent()"; } function revertcontent(){ document.getElementById('content').src='img/content.jpg'; document.getElementById('zentrum').onmouseover = "showvita()"; } function dias( klick ){ document.getElementById('content').src='img/'+klick+'.jpg'; document.getElementById('zentrum').onmouseover = 'null'; document.getElementById('zentrum').onmouseout = 'null'; } 

 <div class="cont" > <div id="zentrum" onmouseover="showvita()" onmouseout="revertcontent()"> <img src="img/content.jpg" id="content" /> </div> </div> 

Now I get stuck with the vita.jpg after taking my mouse out of the div. 现在,将鼠标移出div后,我陷入了vita.jpg的困境。 And after the function dias has been called and thereafter again revertcontent, I am stuck with content.jpg and no more mouseover functionality. 在函数dias被调用并随后再次还原内容之后,我陷入了content.jpg的困境,而没有更多的鼠标悬停功能。 So what is happening here? 那么这里发生了什么? Does .onmouseover="showvita()"; 是否.onmouseover="showvita()"; call that function, or set that value in the div for the mouseover option? 调用该函数,还是在div中为mouseover选项设置该值? I really just need that value reset, I think. 我认为我真的只需要重置值即可。

Generally, it's bad practice to try to change a mouseover attribute. 通常,尝试更改鼠标悬停属性是一种不好的做法。 An easier way would be to set a variable, like "isMouseoverEnabled", and then in your mouseover handler, if the variable is false, you return immediately. 一种更简单的方法是设置一个变量,例如“ isMouseoverEnabled”,然后在鼠标悬停处理程序中,如果该变量为false,则立即返回。 Also, I see that your code is calling revertContent every time the mouse goes over it, which is adding tons of duplicate event listeners. 另外,我看到您的代码每次在鼠标revertContent过它时都会调用revertContent ,这会添加大量重复的事件侦听器。

Alternatively, if you bind your events using javascript instead of a mouseover attribute, it's trivial to unbind them later. 另外,如果您使用JavaScript而不是mouseover属性来绑定事件,那么稍后取消绑定是很简单的。 For example, your code might look like this: 例如,您的代码可能如下所示:

$(document).ready(function() {
    var onMouseOver = function(){
        // Do stuff here
    };

    var revertcontent = function() {
        $('#content')[0].src ='img/content.jpg';

        // We're calling "off" just in case "dias" hasn't been called,
        //    to avoid double event binding
        $('#zentrum').off('mouseover', onMouseOver )
                     .on('mouseover', onMouseOver );
    };
    var dias = function(klick) {
        $('#content')[0].src ='img/content.jpg';
        $('#zentrum').off('mouseover', onMouseOver );
    };

    revertcontent();
});

As a sidenote, jquery really is better for stuff like this because it standardizes things across all browsers. 附带一提,jquery确实更适合诸如此类的东西,因为它可以在所有浏览器中实现标准化。 It allows you to spend more time writing code and less time trying to figure out the subtle differences in mouse events. 它使您可以花费更多的时间编写代码,而花费更少的时间来找出鼠标事件中的细微差别。

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

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