2010-12-10 2 views
3

私は多くのことを検索して試しましたが、これを動作させることはできません。私は以下を持っています:ajaxレスポンスで返されるhtmlを操作するにはどうすればよいですか?

$.ajax({ 
    url: 'g.html', 
    type: 'GET', 
    dataType: 'html', 
    timeout: 4000, 
    cache: false, 
    error: function(request, status, error){ alert('Error'); }, 
    success: function(html){ 
     alert(html); 
     var newData=$('#baa', html); 
     $('#result').html('Blah'+newData); 
    } 
}); 

警告(html)は、g.htmlに含まれるhtmlを出力します。私は次にid = 'baa'の内容を読み込み、それをメイン文書のid = 'result'に移動しようとしています。私の最終目標は、そこに配置される前にそのデータの一部を操作することですが、この単純なバージョンを動作させることはできません。

g.htmlは私が警告(HTML)をすれば

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 TRANSITIONAL//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
    <title>Testing</title> 
</head> 
<body> 
    <div id='baa'> 
     <ul> 
      <li>list 1</li> 
      <li>list 2</li> 
      <li>list 3</li> 
     </ul> 
    </div> 
</body> 
</html> 

は私が試してみて、あるnewDataで何をすれば私は[オブジェクトを取得し、完全なHTML出力を得る....このようになりますオブジェクト]

完全に困惑している、本当にありがとうございました!

答えて

3

試して...

success: function(html){ 
      var $html = $(html); 
      $('#result').html($html.filter('#baa').html());   
     } 
+0

注 - なぜ 'filter'が使われるのか他の回答へのコメントを参照してください。 – fearofawhackplanet

+0

これは治療に感謝します。同様の方法でと<head>のコンテンツにアクセスする方法はありますか? – <span class="text-secondary"> <small> <a rel="noopener" target="_blank" href="https://stackoverflow.com/users/537807/">Simon</a></span> <span></span> </small> </span> </p> </div> </div> </div> <div itemprop="comment" class="post-comment"> <div class="row"> <div class="col-lg-1"><span class="text-secondary">+1</span></div> <div class="col-lg-11"> <p class="commenttext">タイトルテキストを得ることができます: '$ html.filter( 'title').html()'。私は 'head'要素を得る良い方法を知らない。 'html'、' head'、 'body'要素はjQueryのコンストラクタ' $(html) 'で取り除かれていますので、'#baa'を見つけるのに 'filter'が必要です'$ html'(そして' find'はトップレベルの要素の中で*を検索します)の中のレベル要素です。希望は意味をなさない! – <span class="text-secondary"> <small> <a rel="noopener" target="_blank" href="https://stackoverflow.com/users/207752/">fearofawhackplanet</a></span> <span></span> </small> </span> </p> </div> </div> </div> </div> </div> </article> <div> <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-6208739752673518" data-ad-slot="1038284119" data-ad-format="auto" data-full-width-responsive="true"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div> <article class="board-top-1 padding-top-10"> <div class="post-col vote-info"> <span class="count">0<i class="fa fa-thumbs-up"></i></span> </div> <div class="post-offset"> <div class="answer fmt"> <p>試してみてください。</p>また <pre><code class="prettyprint-override">var newData=$(html).find('#baa'); $('#result').html('Blah'+newData.html()); </code></pre> <p>、何が起こっているかの詳細を参照する...</p> <pre><code class="prettyprint-override">alert(newData.html()); </code></pre> <p>を試してみる価値(つまり<code class="prettyprint-override">[object object]</code>を防ぐ必要があります出力)。</p> </div> <div class="post-info"> <div class="post-meta row"> <p class="text-secondary col-lg-6"> <span class="source"> <a rel="noopener" target="_blank" href="https://stackoverflow.com/q/4408958">出典</a> </span> </p> <p class="text-secondary col-lg-6"> <span class="float-right date"> <span>2010-12-10 12:49:09</span> <a rel="noopener" target="_blank" href="https://stackoverflow.com/users/338803/">sje397</a></span> </p> <p class="col-12"></p> <p class="col-12"></p></div> </div> <!-- comments --> <div class="comments"> <div itemprop="comment" class="post-comment"> <div class="row"> <div class="col-lg-1"><span class="text-secondary">+0</span></div> <div class="col-lg-11"> <p class="commenttext">'find 'はここで動作するとは思えません。'#baa'は解析されたjQueryオブジェクトのトップレベルの要素ですので、 'filter'が必要です(私の答えの例を参照) – <span class="text-secondary"> <small> <a rel="noopener" target="_blank" href="https://stackoverflow.com/users/207752/">fearofawhackplanet</a></span> <span></span> </small> </span> </p> </div> </div> </div> <div itemprop="comment" class="post-comment"> <div class="row"> <div class="col-lg-1"><span class="text-secondary">+0</span></div> <div class="col-lg-11"> <p class="commenttext">@fearofawhackplanet - '#baa' 'body'要素の子です。 – <span class="text-secondary"> <small> <a rel="noopener" target="_blank" href="https://stackoverflow.com/users/338803/">sje397</a></span> <span></span> </small> </span> </p> </div> </div> </div> <div itemprop="comment" class="post-comment"> <div class="row"> <div class="col-lg-1"><span class="text-secondary">+0</span></div> <div class="col-lg-11"> <p class="commenttext">'body'要素は' $(html) 'に存在しません – <span class="text-secondary"> <small> <a rel="noopener" target="_blank" href="https://stackoverflow.com/users/207752/">fearofawhackplanet</a></span> <span></span> </small> </span> </p> </div> </div> </div> </div> </div> </article> </div> <div class="clearfix"> </div> <div> <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <ins class="adsbygoogle" style="display:block" data-ad-format="autorelaxed" data-ad-client="ca-pub-6208739752673518" data-ad-slot="1575177025"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div> <div class="padding-top-10"></div> </div> </div> <script type="text/javascript" src="http://img.uwenku.com/uwenku/script/side.js?t=1644592048742"></script> <script type="text/javascript" src="http://img.uwenku.com/uwenku/plugin/highlight/highlight.pack.js"></script> <link href="http://img.uwenku.com/uwenku/plugin/highlight/styles/docco.css" media="screen" rel="stylesheet" type="text/css" /> <script type="text/javascript"> $('pre').each(function(i, e) { hljs.highlightBlock(e, "<span class='indent'> </span>", false) }); </script> <div class="col-lg-3 col-md-4 col-sm-5"> <div id="rightTop"> <div class="row"> <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-6208739752673518" data-ad-slot="5415218910" data-ad-format="auto" data-full-width-responsive="true"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div> <div class="row sidebar panel panel-default"> <div class="panel-heading font-bold"> 最新の質問 </div> <div class="m-b-sm m-t-sm clearfix"> <ul class="side_article_list"> <li class="side_article_list_item"> 1. <a href="http://ja.uwenku.com/question/p-hrkqlkqs-hc.html" target="_blank" title="pycharm_helpersのリポジトリがあります"> pycharm_helpersのリポジトリがあります </a> </li> <li class="side_article_list_item"> 2. <a href="http://ja.uwenku.com/question/p-zdaxuptm-hg.html" target="_blank" title="check_box_outlineアイコンには余分なスペースが必要です"> check_box_outlineアイコンには余分なスペースが必要です </a> </li> <li class="side_article_list_item"> 3. <a href="http://ja.uwenku.com/question/p-dfqawqgn-go.html" target="_blank" title="Nodejs - 別のファイルのSQLクエリ?"> Nodejs - 別のファイルのSQLクエリ? </a> </li> <li class="side_article_list_item"> 4. <a href="http://ja.uwenku.com/question/p-zrwfiplw-gu.html" target="_blank" title="Androidのスプラッシュ画面Firebase同期"> Androidのスプラッシュ画面Firebase同期 </a> </li> <li class="side_article_list_item"> 5. <a href="http://ja.uwenku.com/question/p-vbryfmbw-gm.html" target="_blank" title="H2計算された列式で自動的に行の使用カウント列を維持する"> H2計算された列式で自動的に行の使用カウント列を維持する </a> </li> <li class="side_article_list_item"> 6. <a href="http://ja.uwenku.com/question/p-xndnkeao-gt.html" target="_blank" title="JSP Javascriptはforeach内の隠れた入力からIDを取得するJSTL"> JSP Javascriptはforeach内の隠れた入力からIDを取得するJSTL </a> </li> <li class="side_article_list_item"> 7. <a href="http://ja.uwenku.com/question/p-mmtojxoe-gq.html" target="_blank" title="複雑なwsdlを使用するためのビルド配列 - PHP"> 複雑なwsdlを使用するためのビルド配列 - PHP </a> </li> <li class="side_article_list_item"> 8. <a href="http://ja.uwenku.com/question/p-vdxlomwf-gz.html" target="_blank" title="角度テンプレートの数学的操作"> 角度テンプレートの数学的操作 </a> </li> <li class="side_article_list_item"> 9. <a href="http://ja.uwenku.com/question/p-pnpivrvd-gc.html" target="_blank" title="プライマリSSL再ルーティングHTTPアドオン"> プライマリSSL再ルーティングHTTPアドオン </a> </li> <li class="side_article_list_item"> 10. <a href="http://ja.uwenku.com/question/p-sjgamdva-pq.html" target="_blank" title="異なるデバイスサイズのCSS"> 異なるデバイスサイズのCSS </a> </li> </ul> </div> </div> </div> <p class="article-nav-bar"></p> <div class="row sidebar article-nav"> <div class="row box_white visible-sm visible-md visible-lg margin-zero"> <div class="top"> <h3 class="title"><i class="glyphicon glyphicon-th-list"></i> 関連する問題</h3> </div> <div class="article-relative-content"> <ul class="side_article_list"> <li class="side_article_list_item">関連する問題はありません^_^</li> </ul> </div> </div> </div> </div> </div> </div> </div><!-- wrap end--> <!-- footer --> <footer id="footer"> <div class="bg-simple lt"> <div class="container"> <div class="row padder-v m-t"> <div class="col-xs-8"> <ul class="list-inline"> <li><a href="http://ja.uwenku.com/contact">お問い合わせ</a></li> <li>© 2020 JA.UWENKU.COM</li> <li><a target="_blank" href="https://beian.miit.gov.cn/">沪ICP备13005482号-4</a></li> <li><script type="text/javascript" src="https://v1.cnzz.com/z_stat.php?id=1280101193&web_id=1280101193"></script></li> <li><a href="http://www.uwenku.com/" target="_blank" title="优文库">简体中文</a></li> <li><a href="http://hk.uwenku.com/" target="_blank" title="優文庫">繁體中文</a></li> <li><a href="http://ru.uwenku.com/" target="_blank" title="поле вопросов и ответов">Русский</a></li> <li><a href="http://de.uwenku.com/" target="_blank" title="Frage - und - antwort - Park">Deutsch</a></li> <li><a href="http://es.uwenku.com/" target="_blank" title="Preguntas y respuestas">Español</a></li> <li><a href="http://hi.uwenku.com/" target="_blank" title="कार्यक्रम प्रश्न और उत्तर पार्क">हिन्दी</a></li> <li><a href="http://it.uwenku.com/" target="_blank" title="IL Programma di chiedere Park">Italiano</a></li> <li><a href="http://ja.uwenku.com/" target="_blank" title="プログラム問答園区">日本語</a></li> <li><a href="http://ko.uwenku.com/" target="_blank" title="프로그램 문답 단지">한국어</a></li> <li><a href="http://pl.uwenku.com/" target="_blank" title="program o park">Polski</a></li> <li><a href="http://tr.uwenku.com/" target="_blank" title="Program soru ve cevap parkı">Türkçe</a></li> <li><a href="http://vi.uwenku.com/" target="_blank" title="Đáp ứng viên">Tiếng Việt</a></li> <li><a href="http://fr.uwenku.com/" target="_blank" title="Programme interrogation Park">Française</a></li> </ul> </div> </div> </div> </div> </div> </footer> <!-- / footer --> <script> var _hmt = _hmt || []; (function() { var hm = document.createElement("script"); hm.src = "https://hm.baidu.com/hm.js?f78a970f17b19a79fc477a3378096f29"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(hm, s); })(); </script> </body> </html>