jquery .click not working = jquery .on(点击...)工作正常 [英] jquery .click not working = jquery .on(click...) working fine

查看:149
本文介绍了jquery .click not working = jquery .on(点击...)工作正常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几个星期前,我正在处理我的项目,并写了一些代码,使表行可点击(一直在那里,做)。
此代码以前使用过没有问题。一天是浪费了试图让代码工作(即使我已经做了多次之前) - 一些我试图让它工作的东西 - 改变id到类上的一切, b - 将代码从脚本部分底部移动到顶部, c - 重命名变量, d - 在变量前添加var var从变量前面 e - 将所有单引号改为双引号,将所有双引号改为单引号, f - 更改表的名称, g - 尝试点击表格而不是行, h - 将表格放在div中,将表格从div中删除等等。

A couple of weeks ago I was working on my project and wrote some code to make a table row clickable (been there, done that). This code had been used before without a problem. A day was wasted trying to get the code to work (even though I had done it multiple times before) - some of the things I tried to get it working were a - change id to class on everything, b - move code from bottom of script section to top, c - rename variables, d - add 'var' in front of variables, subtract var from in front of variables, e - change all single quotes to double quotes, change all double quotes to single quotes, f - change the name of the table, g - try to click the table instead of the row, h - put the table in a div, take the table out of a div, etc etc etc.

没有什么是成功的,所以作为最后的手段,深夜,我把.click切换到.on(点击 - 惊喜惊喜 - 一切工作正常。

Nothing was successful, so as a last resort, late at night, I switched the .click to .on(click - and surprise surprise - everything worked fine.

昨天发生了同样的事情,但是不是 - h ,而是将.click改为on。 / p>

以下是两个代码片段:



The same thing happened yesterday, but instead of doing a - h , I jumped right to changing the .click to on(click.

$("#mdtable tr").click(function() { 
    var localmdid = $(this).find("#mdid").html();
    alert("mdid =" + localmdid);
});







$(document).on('click', '#mdtable tr', function(e) {
    var localmdid = $(this).find("#mdid").html();
    alert("mdid =" + localmdid);
           });






上一个NEVER工作,下一个ALWAYS

Here is a code snippet from another page that works just fine:

这是另一个页面的代码片段,工作正常:

$("#patienttable tr").click(function() { var passthis = $(this).find("#localid").html(); $.post("php/setsessionvariable.php", {sessionval: passthis}, function(e) {window.location.href = "root.php"} ); });

I've read the jQuery documentation and it says that the .click function is somehow directly related to the .on(click, and therefore to me, if one works, then other should work.

我已经阅读jQuery文档,它说.click函数是以某种方式直接与

Being a noob, I would like to become a better programmer, and even though my code works with .on(click, I'd like to know why my .clicks don't work (an intellectual exercise).

作为一个noob,我想成为一个更好的程序员,尽管如此,我的代码与.on(点击,我想知道为什么我的.clicks不工作(一个智力锻炼)。

So my questions are:

所以我的问题是: / p>

  1. Are .click and .on(click really related?


  1. Are .click和.on(点击真的相关吗?

    在点击代码段中,我缺少一些使其无法使用的内容?

Is there something obvious I'm missing in the click snippet that makes it non-functional?

有关于此问题有不同的解决方法

Is there a different way to this about this issue?

我再次感谢你

推荐答案

.click 会将点击事件绑定到当时出现在 DOM ,而 on()会委托该事件,并会绑定稍后动态添加的点击(事件)

The .click will bind click event to elements that are present at that time in the DOM whereas the on() will delegate the event and will bind the click (event) which are dynamically added later on.


委托活动的优点是,从
子元素,稍后添加到文档中。通过
选择一个元素,保证在
委托事件处理程序附加时出现,您可以使用委派的事件到
,避免频繁附加和删除事件处理程序, a href =http://api.jquery.com/on/>参考。

这篇关于jquery .click not working = jquery .on(点击...)工作正常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆