jQuery中的第一次单击未触发click事件 [英] click event not triggered at the first click in jquery

查看:122
本文介绍了jQuery中的第一次单击未触发click事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个main.php,其中另一个页面(page1.php)被称为嵌入div中. 加载page1.php时,我正在动态更新page1中的一个表.

I have a main.php inside which another page (page1.php) is being called embedded in a div. I am dynamically updating the a table in page1 when the page1.php is loaded.

现在,当我在表的click事件上调用函数时,会调用该函数,但控件不会进入在函数内部调用的表的click事件中. 但是,第二次单击该控件将进入并正常工作.

Now when I call a function on the click event of the table the function is called but the control doesn't enters in the click event of the table which I have called inside the function. However clicking the second time onwards the control enters and works fine.

以下是js文件中的功能

Following is the function in the js file

function highlight_table()
{
$(#table tr).click(function(){
alert("here");
});

推荐答案

请确保在加载DOM时正在调用此函数,以便第一次附加事件处理程序:

Make sure you are calling this function when the DOM is loaded so that the event handler is attached the first time:

$(function() {
    highlight_table();
});

如果在加载DOM时不存在table,但随后使用AJAX将其添加,则可能需要在AJAX请求的success处理程序中调用此函数.

If the table is not present when the DOM is loaded but is later added using AJAX you might need to call this function in the success handler of your AJAX request.

此外,您可能需要使用 .live() 函数,以便在更新表时点击处理程序将保留(如果需要):

Also you might need to use the .live() function so that when the table is updated the click handler is preserved (if needed):

$('#table tr').live('click', function() {
    alert('here');
});

这篇关于jQuery中的第一次单击未触发click事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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