代码之家  ›  专栏  ›  技术社区  ›  Joshkunz

无法在jQuery中操作从ajax请求插入的数据

  •  2
  • Joshkunz  · 技术社区  · 15 年前

    调用服务器并将返回的数据转换为表之后。我使用jQuery的.html()语法将它插入到页面中。之后,数据出现在页面中,但我无法使用jQuery对其进行操作。

    代码如下:

    <html>
    <head>
        <title>testJavaScript</title>
    
        <script type="text/javascript" src="jquery-1.4.2.js"></script>
    
        <script type="text/javascript">
    
        function makeTable(data) {
    
            var htmlOut = "<table id=\"AjaxTable\">";
    
            for (x in data) {
                htmlOut += "<tr>";
                for (y in data[x]) {
                    htmlOut += "<td>"+data[x][y]+"</td>";
                }
                htmlOut += "</tr>";
            }
    
            htmlOut += "</table>";
    
            return htmlOut;
        }
    
        function getValue() {
            return $("#MyText").val()
        }
    
        $(document).ready(function () {
    
            $("img").hover(function () { //This dosent work on the data returned from the server
                $(this).hide(1000)
                //$(this).css({'background-color': '#357EC7', 'border': '2px solid #2B60DE'});
            })
    
            $("#populateDrop").click(function () {
                $.getJSON('http://127.0.0.1:5000/ajaxTest/json?num='+ getValue() +'&callback=?', function(data) {
                    $(".result").html(makeTable(data.data));
                })
    
            })
        })
        </script>
    </head>
    
    <body>
        <img src="http://www.sharejs.com/code/windows/light-window/gallery/1-nature.jpg" width="50" height="50" /></br>
        <form>
        <input id="MyText" type="text" value="15" />
        </form>
        <a href="#" id="populateDrop">Populate!</a></br>
        <div class="result"></div>
    </body>
    

    1 回复  |  直到 15 年前
        1
  •  0
  •   Sarfraz    15 年前

    你需要使用 live 对于动态数据:

    $("img").live('hover', function () {
     $(this).hide(1000);
    });
    

    更多信息: