jQuery .find()问题

时间:2011-07-14 18:34:33

标签: jquery find

我的问题如下,我使用jquery来获取div内容,但.find()返回null。

我们可以在这里查看源代码:http://webdevs-bg.net/web/html5pload/js.html并打开控制台以查看find()中的日志。

1 个答案:

答案 0 :(得分:2)

您的回复是返回整个HTML文档。不要那样做。您将无法遍历它,因为在将整个文档放入jQuery对象时,不同浏览器的行为会有所不同。

临时修复可能是使用filter()[docs]方法而不是.find()

我猜你在浏览器上进行了测试,除了<body>的内容之外,除了"#content"的内容之外,其他所有内容都被删除,.find()元素不在#content找到它。

请注意,这不适用于所有浏览器,这就是为什么您只需要返回实际需要的内容。

此外,您将$.get(History.getState().url, { text: "123" }, function (response) { // v--filter will work in some browsers, but not all var $content = $response.filter("#content"); $("#content").empty().append($content); // <-- appending #content to #content? console.log('content is: ', $content); }); 的新元素附加到页面上具有相同ID的现有元素。我假设你只想附加孩子。

<h3> JS файл </h3> 

这应该是你的答复:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8"/>
        <script type="text/javascript" src="jquery.js"> </script>
        <script type="text/javascript" src="history/amplify.store.js"> </script>
        <script type="text/javascript" src="history/history.adapter.jquery.js"> </script>
        <script type="text/javascript" src="history/history.js"> </script>
    </head>
    <style type="text/css">
        body {
        padding: 10px;
        }
    </style>
    <body>

        <script>
            var History = window.History;
            $(function() {
            History.Adapter.bind(window,'statechange', function() {
            $.get(History.getState().url, {text: "123"}, 

            function(response) {
            var $response = $(response);
            var $content = $response.find("#content");
            $("#content").empty().append($content);
            console.log($content);

            });
            });
            $("a").click(function() {
            History.pushState(null, null, $(this).attr("href"));
            return false;
            });
            });
        </script>
        <a href="ffmpeg.php">Начало</a> | <a href="js.html">JSинг</a>
        <div id="content">
            <h3> JS файл </h3>  <!-- <<--THIS should be your response -->
        </div>
        <div id="c2">footer</div>
    </body>
</html>

...而是你发送了这个:

{{1}}
相关问题