2013-04-08 65 views
-1

我需要在JavaScript中單擊鼠標後調用鼠標懸停事件而不是jQuery,主要的事情是該功能僅在單擊相同功能後纔會發生相同的事件改變 和它的新事件是鼠標懸停是可能的?如何在javascript中的同一個函數上單擊事件後調用鼠標懸停事件

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

好了,所以有什麼需要在這裏點擊事件首先撥打swapClass(),然後後單擊事件相同的函數將調用onmouseover事件,但請記住,功能paramertarized

+2

如果**不**希望這樣使用jQuery這是爲什麼標籤[標籤:jQuery的]的答案?也不確定AJAX與這些有什麼關係。 – 2013-04-08 10:58:11

+0

首先,我想添加單擊事件任何以及如何條件然後單擊相同的功能後,事件將改變mouseOver – 2013-04-08 10:58:33

+0

,爲什麼你標記jQuery和jQuery的Ajax如果你不想* jquery的方式*? – SachinGutte 2013-04-08 10:58:54

回答

-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=""; 
    } 
}); 
+0

之前等待點擊事件,但@simon看我的問題是我想製作一個像這樣的克隆網站 http://www.wordtwist.org/html5.php? ü= 6cadee0f0cee1363324e5941587c92091365435691.please檢查我並告訴我錯在哪裏請 和我原來的工作是 http://webwingtechnologies.co.in/quooq/games.php – 2013-04-08 12:35:42

+0

對不起@umeshchakor我只編輯了包含代碼的答案。你想問一下user1136403是誰組成的JSFiddle。 – 2013-04-08 12:51:38

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); 
     }); 
    }); 
} 
+0

其實@jeff首先我刪除頁面加載功能或不..我不確認告訴我嗎? – 2013-04-08 11:33:53

+0

@umeshchakor你爲什麼要刪除網頁加載功能?它們不會在頁面加載時存在。當JS運行時,它將添加click事件監聽器。然後,一旦你點擊,它將刪除點擊監聽器並添加鼠標懸停監聽器。也許我只是不理解問題 – 2013-04-08 11:35:49

+0

,但@jeff我沒有得到Hi,因爲數字ID是在while循環中顯示的代碼?我如何得到修復數字ID? – 2013-04-08 11:43:49