2012-03-24 74 views
0

如何插入jQuery的HTML標記內使用它在onclick。例如,我怎樣才能在html中插入這個jquery函數?在html標籤內使用jquery?

<html> 
<head> 
<script type="text/javascript" src="jquery.js"></script> 
<script type="text/javascript"> 
$(document).ready(function(){ 
    $("button").click(function(){ 
    $("p").slideToggle(); 
    }); 
}); 
</script> 
</head> 
<body> 
<p>This is a paragraph.</p> 
<button>Toggle between slide up and slide down for a p element</button> 
</body> 
</html> 
+0

你想要做什麼?你現在的代碼不工作嗎? – JJJ 2012-03-24 14:07:09

+3

您應該閱讀一些jQuery教程:http://docs.jquery.com/Tutorials – 2012-03-24 14:15:55

+1

使用內聯事件處理程序會導致您網站的指數級難度維護,您應該*努力避免如果可能的話,並保持你的行爲超出你的標記。 – 2012-03-24 14:22:50

回答

1

直接把你的函數體中button標籤作品的onclick屬性。

<button onclick='$("p").slideToggle();'> 
    Toggle between slide up and slide down for a p element 
</button> 

不過,我想你是做它最好的方式的方式,因爲它需要的$(document).ready功能的優勢。

+0

非常感謝:) – Sarah 2012-06-27 10:01:13