<!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)方法
原文地址:https://www.cnblogs.com/fps2tao/p/8254055.html