A-A+
Jquery中live支持多次click事件的实例
live() 方法为被选元素附加一个或多个事件处理程序,并规定当这些事件发生时运行的函数。通过 live() 方法附加的事件处理程序适用于匹配选择器的当前及未来的元素(比如由脚本创建的新元素)。
Jquery中live支持多次click事件的实例如下:
- <script>
- $(function(){
- $('#public_ym_left').live('click',function(){
- //<符号的右侧的第一个号
- var sibQ=$('#public_ym_left').next().text();
- if(sibQ>=1&&sibQ<=5) var start=1;
- if(sibQ>5)
- var start=parseInt(sibQ)-4;
- $("#upage").children().remove();
- var childQ=$('<li id="public_ym_left"><a href=\'#\'" /a></li>');
- var childH=$('<li id="public_ym_right"><a href=\'#\'" /a></li>');
- var loage=$('<li><a href=\'#\'" /a></li>');
- $("#upage").append(childQ);
- for(var i=start;i<(start+5);i++)
- {
- var son=$('<li><a id="page'+i+'" href=\'#\'" /a></li>');
- $("#upage").append(son);
- }
- $("#upage").append(loage);
- $("#upage").append(childH);
- $('#public_ym_left').unbind();
- });
- $('#public_ym_right').live('click',function(){
- var sibH=$('#public_ym_right').prev().prev().text();
- var start=parseInt(sibH);
- $("#upage").children().remove();
- var childQ=$('<li id="public_ym_left"><a href=\'#\'" /a></li>');
- var childH=$('<li id="public_ym_right"><a href=\'#\'" /a></li>');
- var loage=$('<li><a href=\'#\'" /a></li>');
- $("#upage").append(childQ);
- for(var i=start;i<(parseInt(start)+5);i++)
- {
- var son=$('<li><a id="page'+i+'" href=\'#\'" /a></li>');
- $("#upage").append(son);
- }
- $("#upage").append(loage);
- $("#upage").append(childH);
- $('#public_ym_right').unbind();
- });
- });
- </script>
- <div>
- <ul id="upage">
- <li id="public_ym_left"><a href=\'#\'" ></a></li>
- <li><a id="page1" href=\'#\'" /a></li>
- <li ><a id="page2" href=\'#\'" /a></li>
- <li><a id="page3" href=\'#\'" /a></li>
- <li ><a id="page4" href=\'#\'" /a></li>
- <li ><a id="page5" href=\'#\'" /a></li>
- <li><a href=\'#\'" /a></li>
- <li id="public_ym_right"><a href=\'#\'" /a></li>
- </ul>
- <table border="0" cellpadding="0" cellspacing="0">
- <tr>
- <td width="64">跳转到第</td>
- <td width="57"><input name="" type="text" /></td>
- <td width="59">共<span>100</span>页</td>
- </tr>
- </table>
- </div>
Jquery中click事件重复执行的问题
问题:点击修改链接,在弹出的对话框中,点提交按钮,click事件会执行多次。而用原生js则没有问题,并且提交后,第二次提交后,多个数据都变成一个了,个人认为原因是:click事件里不能包含click事件?不知道是不是这样,为方便查看,附带整个文档.
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh-CN">
- <head>
- <title>Document</title>
- <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
- <meta name="description" content="" />
- <meta name="keywords" content="" />
- <style type="text/css">
- * {margin: 0; padding: 0;}
- #table {border: 1px solid gray; border-colla<a href="/fw/photo.html" target="_blank">ps</a>e: collapse; width: 500px;}
- tr {height: 30px;}
- th {border: 1px solid gray;}
- td {border: 1px solid gray;text-align: center;}
- td a {margin-right: 5px; color: blue; text-decoration: none;}
- #popDiv, #editDiv {border: 1px solid silver; width: 315px; padding: 1px; margin-top: 10px; position: absolute; left: 38%; z-index: 4; display: none;}
- .pop p {height: 30px; margin-top: 20px; clear: both;}
- .pop a {display: block; float: right; text-decoration: none; font-size: 12px;}
- .pop .input {height: 20px; line-height: 20px;}
- /*#bottom {width: 100%; height: 30px; margin: 0 auto;}*/
- #sub {display: block; height: 30px; margin: 0 auto;}
- .mask {background-color: #000; position: absolute; left: 0; top: 0; z-index: 2;}
- </style>
- <script type="text/<a href="/js_a/js.html" target="_blank">javascript</a>" src="//ajax.aspnetcdn.com/ajax/jQuery/jquery-1.11.0.min.js"></script>
- </head>
- <body>
- <table id="table">
- <tr>
- <th>姓名</th>
- <th>年龄</th>
- <th>职位</th>
- <th>工资</th>
- <th>操作</th>
- </tr>
- <tr>
- <td>张三</td>
- <td>23</td>
- <td>PHP</td>
- <td>79999</td>
- <td><a href="#">修改</a></td>
- </tr>
- <tr>
- <td>李四</td>
- <td>21</td>
- <td>Java</td>
- <td>12000</td>
- <td><a href="#">修改</a></td>
- </tr>
- <tr>
- <td>王五</td>
- <td>34</td>
- <td>Python</td>
- <td>29999</td>
- <td><a href="#">修改</a></td>
- </tr>
- <tr>
- <td>赵六</td>
- <td>37</td>
- <td>Javascript</td>
- <td>65450</td>
- <td><a href="#">修改</a></td>
- </tr>
- </table>
- <div id="editDiv">
- <a href="#">close</a>
- <p><strong>姓名:</strong><input type="text" /></p>
- <p><strong>年龄:</strong><input type="text" /></p>
- <p><strong>职位:</strong><input type="text" /></p>
- <p><strong>工资:</strong><input type="text" /></p>
- <input type="button" id="sub" value="提交" />
- </div>
- <script type="text/javascript">
- // 点击'修改'链接
- $('a.edit').click(function () {
- var arr = [];
- $(this).parent().siblings().each(function () {
- arr.push($(this).text());
- });
- $('#editDiv').show().find('p').each(function (i) {
- $(this).find('input:text').val(arr[i]);
- });
- var aTr = $(this);
- $('#sub').click(function () {
- alert('2222222');
- var data = [];
- $(this).prevUntil('a.close').each(function () {
- data.push($(this).find('input:text').val());
- });
- data.reverse();
- aTr.parent().siblings().each(function (i) {
- $(this).text(data[i]);
- });
- $(this).parent().hide();
- $('div.mask').remove();
- });
- // 原生JS实现点击,没有问题
- /*document.getElementById('sub').onclick = function () {
- alert('1111111');
- var data = [];
- $(this).prevUntil('a.close').each(function () {
- data.push($(this).find('input:text').val());
- });
- data.reverse();
- aTr.parent().siblings().each(function (i) {
- $(this).text(data[i]);
- });
- $(this).parent().hide();
- $('div.mask').remove();
- };*/
- // 添加遮罩层
- var maskHeight = $(document).height();
- var maskWidth = $(document).width();
- $('<div></div>').appendTo($('body'));
- $('div.mask').css({
- 'width':maskWidth,
- 'height':maskHeight,
- 'opacity':0.4
- });
- });
- $('a.close').click(function () {
- $(this).parent().hide();
- $('div.mask').remove();
- });
- </script>
- </body>
- </html>
回答一:已经找到原因了,我提供个方法吧:
- $('#sub').unbind('click').click(function () {
- ...
- });
每次绑定前先取消上次的绑定。
回答二:找了好久,发现原因应该是:
Click事件 进行了累加绑定,每当我调用一次时,他便增加一次绑定,按这个方法,确实解决了问题。不知道是否正确。