2011-07-14 5 views
1

私の問題は次のとおりです。私はdivコンテンツを取得するのにjqueryを使用しますが、.find()はnullを返します。jQuery .find()の問題

ソースコードはhttp://webdevs-bg.net/web/html5pload/js.htmlで表示され、コンソールを開いてfind()からログを表示できます。

+0

。 –

+0

コンソールを開き、リンクをクリックしてください。JSとあなたがJSをクリックしているときは、をクリックしてください。 –

答えて

2

あなたの応答はHTMLドキュメント全体を返しています。それをしないでください。ドキュメント全体をjQueryオブジェクトにドロップすると、ブラウザによって動作が異なるため、トラバースに問題があります。

.find()の代わりにfilter()[docs]メソッドを使用することが一時的に発生することがあります。

のコンテンツ以外のすべてを取り除いて、が見つからないトップレベルの"#content"要素を残しているブラウザでテストしていると思います。

これはすべてのブラウザでは機能しないため、実際に必要なコンテンツのみを返す必要があることに注意してください。

また、同じIDを持つページの既存の要素に#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); 
}); 

これはあなたの応答する必要があります:

<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>