2012-03-24 43 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