如何在javascript中的相同功能上单击事件后调用鼠标悬停事件

时间:2013-04-08 10:53:21

标签: javascript

我需要在JavaScript中点击鼠标而不是jQuery后调用鼠标悬停事件,主要的是该函数在点击相同的函数后才会相同事件将被更改 它的新事件是鼠标悬停可能吗?

<a href="javascript:void(0);" id="digit" 
   onClick="javascript:return swapClass(val,val2);" 
   class="GetDivCount" onMouseOver="javascript:return swapClass(val,val2);"> 

好的,这里需要在click事件上调用第一个swapClass()然后在click事件之后,同一个函数将调用onmouseover事件但是记住该函数是参数化的

2 个答案:

答案 0 :(得分:0)

如果我理解正确,您只想在点击事件发生后等待onmouseover ...为此,您必须摆脱内联事件处理程序。以下示例应该有效。您必须区分浏览器是否可以使用addEventListener,因为旧版本的IE不支持它。

HTML:

<a href="javascript:void(0);" id="digit" class="GetDivCount">Hi</a>

JS:

var a = document.getElementById('digit');
if (a.addEventListener) {  // Most browsers....
    a.addEventListener('click', function test() {
        // Add code to determine val and val2
        swapClass(val,val2);
        a.removeEventListener('click', test, false); // Remove click listener
        a.addEventListener('mouseover', function() { // Add mouseover listener
            swapClass(val,val2);
        }, false);
    }, false);
} else { // Older versions of IE only support attachEvent :(
    a.attachEvent('onclick', function test() {
        // Add code to determine val and val2
        swapClass(val,val2);
        a.detachEvent('onclick', test);
        a.attachEvent('onmouseover', function() {
            swapClass(val,val2);
        });
    });
}

答案 1 :(得分:-1)

这里有两个可能的解决方案,使用jQuery而不使用jQuery:JSFiddle

var control = "";
var first  = document.getElementById("first");
var second = document.getElementById("second");

first.onclick = function(){
    control = true;
    alert("clicked | doSomething Here");
}
second.onmouseover = function(){
        if(control==true){
        alert("mouseovered after click | doSomething Here");
        //if you want again click and after mouseover evenet
        //you have to put here
        //control=""; 
    }
}

使用:

$("#first").on("click",function(){
    control = true;
    alert("cilcked | doSomething Here");
});

$("#second").on("mouseover",function(){
    if(control==true){
        alert("mouseovered | doSomething Here");
        //if you want again click and after mouseover evenet
        //you have to put here
        //control="";
    }
});