1. 程式人生 > >jquery的onclick(this)方法

jquery的onclick(this)方法

ali ext attr inpu title lns span asc tran

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
<html xmlns="http://www.w3.org/1999/xhtml">  
<head>  
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
<title>無標題文檔</title>  
<
style type="text/css"> .user_btn { width: 60px; height: 30px; background: #FC9; -moz-border-radius: 15px; -webkit-border-radius: 15px; border-radius: 15px; text-align: center; } </style> <script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js"
></script> <script type="text/javascript"> function shoucang(which){ if($(which).attr("value")=="收藏") { $(which).attr("value","已收藏"); } else if($(which).attr("value")=="已收藏") { $(which).attr("value",
"收藏"); } } </script> </head> <body> <input name="收藏" type="button" value="收藏" class="user_btn" onclick="shoucang(this)"/> </body> </html>

初學者使用onclick時,要註意幾點:

1、onclick屬性的正確寫法是onclick="函數名()",它是帶括號的,而不是onclick="函數名"

2、如果onclick屬性沒有傳入this對象,則在函數定義中不能使用$(this),否則解釋器會因為找不到該對象而停止運行。

轉:http://blog.csdn.net/qq_26222859/article/details/51446900

jquery的onclick(this)方法