2013-03-20 79 views
0

我試圖使用來自dynamicdrive.com的顯示提示腳本在我的網站上顯示錶單錯誤。jquery mouseover事件不起作用

目前當存在與一個輸入錯誤顯示錯誤作爲

<span class="error">Error Message</span> 

我想用節目提示腳本改爲顯示這個錯誤,那麼我將不得不更換

<span class="error">Error Message</span> 

<span class="error erroranchor" onmouseover="showhint('Error Message Here', this, event, '200px')"></span> 
手動

編輯每一個擁有它的錯誤頁面。

但我試圖在這裏節省一些能源,這就是爲什麼我決定使用JQuery代替,我是新來的查詢,這將是我的第一個編碼工具,並從邏輯上看我認爲JQuery將是最好的。這是我迄今爲止所做的,但沒有任何工作。

<script type='text/javascript'> 
//On document ready.. I'm not sure if it should be document ready or document load. 
    $(document).ready(function() { 
//First i add the class "erroranchor" to <span class="error"> 
    $(".error").addClass("erroranchor"); 
//Then i've tried to convert the onmouseover event to JQuery mouseover event. 
    $(".erroranchor").mouseover(showhint(){ 
//Here i try to get the "Error Message" from <span class="error">Error Message</span> so it can be displayed in the hint box. 
     var $errortext = $(this); 
     $errortext.text(), 
     this.event, 
     '200px' 
    }); 
    }); 
</script> 

再有就是我想要的作品完美的CSS ..

.error { 
    text-indent: -99999px; 
} 
.erroranchor { 
    background: url(anchor.png); 
} 

我會很感激,如果任何改進可以在上述的JavaScript進行(請保持它容易理解)。

回答

4

您的mouseover綁定錯誤。它應該是

$(".erroranchor").mouseover(function(){...}); 

注意到它是function而不是showhint

如果你想創建showhint()功能在你的代碼的其他地方自身的功能,那麼結合將是

$(".erroranchor").mouseover(showhint); 

編輯
如果你想爲每個'.erroranchor'不同的錯誤信息,你可以使用jQuery的data()功能:

<span class="error erroranchor" data-errormessage="Error message here"></span> 
<span class="error erroranchor" data-errormessage="Different error message here"></span> 

然後你的事件處理程序和樂趣ction:

$(".erroranchor").mouseover(showhint); 

function showhint() { 
    var errMsg = $(this).data('errormessage'); 
    if(errMsg) { 
     //do stuff 
    } 
} 
+0

感謝您的回答..其實,將會有與他們每個人不同的錯誤信息頁面上有多個.erroranchor ..已經有一個showhint函數,完美的工作...試圖做的是改變錯誤信息使用jquery或任何可能的手段,然後showhint函數將工作。 – 2013-03-20 18:12:39

+0

@eesYboi檢查編輯。 – Marcus 2013-03-20 18:20:33

0

試試這個

$(".erroranchor").mouseover(function(){ 
     showhint(); 
});