简体   繁体   English

将内容动态添加到页面后,为什么我的JS函数不起作用?

[英]Why does my JS function not work after dynamically adding content to the page?

I have a javascript function that is defined in the beginning of my webpage. 我在网页的开头定义了一个javascript函数。 It is called when a row is clicked in my table. 在我的表格中单击某行时会调用它。

here is my function: 这是我的功能:

    $(".info").click(function(){
        alert($(this).attr('movieid'));
    });

here is my what my table row looks like: 这是我的表格行的样子:

<tr class="info" movieid="123"><td>movie title</td></tr>

now that table is displayed when I first load the page, I can click the row and it will call the function correctly. 现在,当我第一次加载页面时会显示该表,我可以单击该行,它将正确调用该函数。 I am trying to implement a search function for the table. 我正在尝试为表实现搜索功能。 I have the php script return table rows just like the one above, I add them to the table, but when I click the row that is returned from the php script, the javascript function isn't called. 我有php脚本返回表行,就像上面的那一行一样,我将它们添加到了表中,但是当我单击从php脚本返回的行时,没有调用javascript函数。 Does anyone know how I can accomplish this? 有人知道我该怎么做吗? I've had no luck. 我没有运气。

EDIT 编辑

here is my function to get the search results: 这是我获取搜索结果的功能:

$.ajax({
        type: "POST",
        url: "www.mysite.com/core/search.php",
        data: { q: search_string},
        cache: false,
        success: function(html){
            $("table#movies").fadeOut();
            $("div#results-container").fadeIn();
            $("div#results").fadeIn();
            $("div#results").html(html);
        }
});

You are adding the element dynamically so it is not registered when you set the click handler. 您正在动态添加元素,因此在设置点击处理程序时不会注册它。

$("body").on("click", ".info", function(e) {
   ...
});

body usually works but ideally you'd want a DOM element as close to your selector .info as possible. body通常可以使用,但理想情况下,您希望DOM元素尽可能靠近选择器.info

You have to do this: 您必须这样做:

$("body").on("click", "tr", function(event){
    alert($(this).attr('movieid'));
});

By adding a "container", it will have an effect like .live() . 通过添加“容器”,它将具有类似于.live()的效果。

Use j Query ready event 使用j查询就绪事件

$(document).ready(function() {    
    $(".info").on("click", "tr", function(event){
       alert($(this).attr('movieid'));
   });
});

Add .on on document because you are adding DOM to document. document上添加.on ,因为您.on DOM添加到文档中。

Example

$(document).on('click',".info",function(){
       alert($(this).attr('movieid'));
});

You php script is most likely not adding the class to the table row. 您的php脚本很可能没有将类添加到表行。 Alter it to add that class. 对其进行更改以添加该类。

Thanks for the help from all of you guys. 谢谢大家的帮助。 I was able to figure it out.. @jasen mentioned that you to declare it as a body tag. 我能够弄清楚。.@ jasen提到您将其声明为body标签。 Here was the solution I was able to use: 这是我可以使用的解决方案:

$("body").on("click", ".info", function(event){
   alert($(this).attr('movieid'));
});

Thank you Derek as well.. you provided some useful examples for the future. 谢谢德里克(Derek),您也提供了一些有用的示例。

First, specify a selector where all the tr.info node will be contained, now and in future. 首先,指定一个选择器,现在和将来将包含所有tr.info节点。 From your latest edit of the post, I believe that's table#movies . 从您对帖子的最新编辑中,我相信它是table#movies

Then attach event with on and specify the children selector tr.info : 然后将事件附加到on并指定子选择器tr.info

$('table#movies').on('click', 'tr.info', function () {
    alert($(this).attr('movie'));
});

See example on jsFiddle 参见jsFiddle上的示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我的页面重定向不起作用? - Why my page redirection does't work? 为什么我的新内容没有立即显示/消失在页面上,而是在手动刷新后出现了? - Why is my new content not showing/going away on the page instantly, but does after a manual refresh? 为什么即使在上面进行连接后,我在函数内与数据库的连接仍无法正常工作? - Why does not my connection to my database inside a function work even after I've connected above? 为什么我的href仅在手动刷新页面后才起作用? - Why does my href only work after I manually refresh the page? 为什么我的 checkall 功能不起作用? - Why does my checkall function not work? 用cloudfare添加SSL后,JS不工作,网络出现这个错误, - After adding SSL with cloudfare, the JS does not work and this error occurs in Network, {{content()}}函数在伏特页面Phalcon框架中如何工作 - how does a {{content()}} function work in volt page phalcon framework AJAX:使用ajax加载内容后,JavaScript函数不起作用 - AJAX: Javascript function does not work after content is loaded using ajax 为什么我的网站加载带有旧内容的js文件? - Why does my websites loads js file with old content? 为什么聚合功能在laravel SQL查询中不起作用? - Why does aggregation function does not work in my laravel SQL Query?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM