dqed19166 2014-09-05 06:39
浏览 40
已采纳

无法访问从AJAX调用追加的元素

I insert element from the php file as a result of successful ajax call. But for some reason that and everything in it is missing when I look at my source code of the page. It appears on the page correctly but it isn't clickable or anything until I refresh my page! How can I make elements usable right after successful AJAX call?

Jquery AJAX:

$( "#addvideo" ).click(function() {       
    $.ajax({
      type: "POST",
      datatype: 'json',
      url: "/public/index.php/admin/content/athletes/addvideo",
      data: { youtube: $("input[name=youtube]").val(), vid:$("input[name=athletes_vid]").val(), ci_csrf_token: $("input[name=ci_csrf_token]").val() }
    })
    .done(function( data ) {
      $('#youtubeholder').last().append(data);    
    });  
}); 

This call appends following data from php controller:

$jquery_data = "<div style='float: left; width: 200px; height: 200px' id='video_pic'>
                    <img id='".$id."' src='".img_path().$tb_data->filename."' /><br/>
                    <div class='btn deletevid' style='width: 145px; margin-left: auto; margin-right: auto;'>DELETE</div>
                </div>";

echo $jquery_data; 

To the <div id="youtubeholder"> on the same page as AJAX call. However nothign happens when I click the "DELETE" button.

  • 写回答

3条回答 默认 最新

  • dqba94619 2014-09-05 06:42
    关注

    View Source in the browser only shows what was originally in the HTML of the page as originally downloaded from the server. It does not show content that was dynamically or programmatically added to the page.

    If you want to see the current content of the DOM, then use a DOM inspector as is built into nearly all browsers now. In Chrome, you can right-click, select "Inspect Element", click on the "Elements" tab and see the entire DOM hieararchy (you may need to expand the parts you want to see).


    When you say "isn't clickable", it isn't entirely clear what you mean. If you mean that your click handlers are not bound to the elements that were dynamically added to the page, that is likely to be expected because you probably ran some code that looks like this:

    $(some selector).click(function() {
        // code here
    });
    

    That code binds click handlers ONLY to the elements that exist at that moment, not to future elements that might match the selector.

    You can work-around that issue in one of several ways:

    1. You can manually bind events to the newly added elements.
    2. You can use delegated event handling (instead of static event handling) that will work with dynamically added elements.

    Dynamic event handling in jQuery works like this:

    $(some static parent selector).on("click", "selector that matches dynamic elements", fn);
    

    For example, this will work with dynamically added items in the #youtubeholder div that have a selector matching .deletevid:

    $("#youtubeholder").on("click", ".deletevid", function() {
        // delete element here
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!