-1

我需要在 JavaScript 而不是 jQuery 中单击鼠标后调用鼠标悬停事件,主要是该功能仅在单击相同功能事件后才会发生变化,并且其新事件是鼠标悬停可能吗?

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

好的,所以这里需要先在单击事件上调用 swapClass(),然后在单击事件之后,相同的函数将调用 onmouseover 事件,但请记住该函数是参数化的

4

2 回答 2

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);
        });
    });
}
于 2013-04-08T11:24:42.293 回答
-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="";
    }
});
于 2013-04-08T11:17:29.353 回答