zoukankan      html  css  js  c++  java
  • Ajax异步获取html数据中包含js方法无效的解决方法

     页面上使用js写了一个获取后台数据的方法

    复制代码
    复制代码
    复制代码
    function data() {
            var tab = $("#dic")
            $.ajax({
                url: '../demo.ashx?method=GetList',
                data: {},
                dataType: 'json',
                type: 'post',
                async: true,
                success: function (data) {
                    //console.log(data);
                    var parentStr = '';
                    $.each(data, function (i, item) {
                        //console.log(item.text);

    parentStr += "<div class='pull-right'> <a class='morechange' href='javascript:;' style='visibility: visible;'>更多+</a></div>"

    });
    tab.html(parentStr);
    }
    })

    }

    复制代码
    复制代码
    复制代码

    其中的

    <a class='morechange' href='javascript:;' style='visibility: visible;'>更多+</a>  绑定一个点击时间

    $('.morechange').click(function(){
        alert("弹出")
    });

    发现点击无效无效

      原来是 ajax载入新dom之前js 就加载完了,事件当然没有绑定到新载入的dom上

    解决方法:

       使用jquery的委托事件,将该方法委托到页面已经存在的一个节点上

    $("#dic").delegate('.morechange', 'click', function () { alert("弹出"); });

     问题解决。

    当然也可以不使用异步将async改为false也是可以的

    -转载

  • 相关阅读:
    VM player无法联网问题
    寄存器
    linux下的文件操作
    linux的切换目录操作
    linux的ls -al指令
    python对ASC码的加减
    ASC码速记
    pyhton的返回值
    intellij 调试方法
    2015,5.10
  • 原文地址:https://www.cnblogs.com/chun6/p/7058463.html
Copyright © 2011-2022 走看看