2013-10-19 54 views
-1
<span id="change"> 
    Stuff In Here 
</span> 

<script> 
$("#change").on("mouseover", function() { 
    $('#change').text("This is the new html"); 
}); 
</script> 

以下不起作用,作爲jQuery noob,我不知道爲什麼。jQuery - 在鼠標懸停改變文字

+0

你能重現一個演示的問題嗎?因爲寫的*應該*的工作(假設''真的*不*遵循它的意圖行事的元素。 –

回答

1

更改此:

<script> 
$("#change").on("mouseover", function() { 
    $('#change').text("This is the new html"); 
}); 
</script> 

因爲你沒有任何引用。試試這個:

<script> 
    $(document).ready(function() { 
    $("#change").mouseover(function() { 
     $('#change').text("This is the new html"); 
    }); 
    }); 
</script> 

那麼,小提琴還有另一個錯誤。你沒有在左上角選擇任何jQuery版本。更新完成後,代碼完美運行,並從中得出結論,您可能沒有在您的網站中鏈接腳本。

嘗試script文件(網站)

這裏現在已經看鏈接在佈局的head部分:http://jsfiddle.net/afzaal_ahmad_zeeshan/HMhVn/3/

我已經更新它,它完美的現在運行:)

+0

什麼是錯誤您聲明是否存在於您的第一個代碼塊中?並且$(document).ready(function(){/*...*/})'是$(function(){/ * ...)的別名* /})(在另一個答案中使用) –

+0

對不起,這是我的錯,而編輯答案! –

+0

我不得不更新答案,添加錯誤的小提琴。jquery版本根本沒有鏈接所以我錯誤地將錯誤添加到頂部! –

1

幹得好! http://jsfiddle.net/HMhVn/6/

如果需要,爲了獲得更大的靈活性,您可以先緩存原始字符串,然後將文本放入變量中,然後再決定在mouseover上做什麼。

HTML:

<span id="change"> 
    Stuff In Here 
</span> 

JQuery的:

$(document).ready() 
{ 
    $("#change").hover(
     function() // on mouseover 
     { 
      $(this).text("This is the new html"); 
     }, 

     function() // on mouseout 
     { 
      $(this).text("Stuff In Here"); 

     }); 
}; 

這可以讓你永久不改它,它的工作要做。就我個人而言,我會編寫它的靈活性。