Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

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.

Following is the function in the js file

function highlight_table()
{
$(#table tr).click(function(){
alert("here");
});
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
183 views
Welcome To Ask or Share your Answers For Others

1 Answer

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();
});

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.

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');
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...