qq_拾梦人_03364242
2016-09-24 07:43
有没有大神能讲解下面这段代码呢,完全看不懂呀
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<title></title>
<style>
.left div,
.right div {
width: 500px;
height: 50px;
padding: 5px;
margin: 5px;
float: left;
border: 1px solid #ccc;
}
.left div {
background: #bbffaa;
}
.right div {
background: yellow;
}
</style>
<script src="http://libs.baidu.com/jquery/1.9.1/jquery.js"></script>
</head>
<body>
<h2>自定义事件trigger</h2>
<div class="left">
<div><span></span><span>0</span>点击次数</div>
<button>直接点击</button>
<button>通过自定义点击</button>
</div>
<script type="text/javascript">
//点击更新次数
$("button:first").click(function(event,bottonName) {
bottonName = bottonName || 'first';
update($("span:first"),$("span:last"),bottonName);
});
//通过自定义事件调用,更新次数
$("button:last").click(function() {
$("button:first").trigger('click','last');
});
function update(first,last,bottonName) {
first.text(bottonName);
var n = parseInt(last.text(), 10);
last.text(n + 1);
}
</script>
</body>
</html>
当第一个点击自定义按钮时,会触发 $("button:last").click(function() {})然后执行里面的 $("button:first").trigger('click','last'),执行到这步又触发第一个button的click事件,function参数中的buttonName=last,继续执行update()函数,第一个参数为第一个 span对象,第二个参数为第二个 span对象,第三个参数的内容此时为 'last'。在update函数中,将第一个span的文本设置为 'first',将第二个span的文本转换为整型,基数为10(这个10不懂看看转换整型那节),计数器n自加 1,并赋给第二个 span的文本。
当点击第一个 button 时,会触发第一个按钮的 click事件,function 参数中的 bottonName,
如果存在bottonName,则bootonName=bottonName,否则,bottonName=first
此时还未定义(undefined),所以在执行"或"语句时,将 'first'这个字符串赋给bottonName,继续执行 update()函数。第一个参数为第一个 span对象,第二个参数为第二个 span对象,第三个参数的内容此时为 'first'。
在update函数中,将第一个span的文本设置为 'first',将第二个span的文本转换为整型,基数为10(这个10不懂看看转换整型那节),计数器n自加 1,并赋给第二个 span的文本。
点击第二个 button的过程以此类推。
前面同学总结的*_*
jQuery基础(三)—事件篇
89987 学习 · 645 问题
相似问题
回答 4
回答 3
回答 1
回答 2
回答 4