2011-12-17 9 views
2

jQueryを使用してAJAXローダーとして動作するページがあります。このようなXML文書をフェッチし、表示されたページの適切なdivtitle,authPhraseおよびcontentの値を挿入します。jQueryはXMLからタグを取り除きます

<?xml version="1.0" encoding="utf-8"?> 
<tinglePage> 
    <title>Home</title> 
    <authPhrase>welcome, Jashank<br /><a href="/tingle/auth/logout">logout</a></authPhrase> 
    <content> 
    <p>There are <strong>two</strong> types of folks who sit around 
    thinking about how to kill people: <em>psychopaths</em> and 
    <em>mystery writers</em>. I'm the kind who pays better.</p> 
    </content> 
</tinglePage> 

このJavaScriptを使用すると、挿入されたコンテンツからタグが取り除かれます。

jQuery.get(path, {ajax: "true"}, function(xml) { 
     document.title = "Tingle :: " + $("title", xml).text(); 
     $(".pageTitle").html(
      $("title", xml).text() 
     ); 

     $(".authPhrase").html(
      $("authPhrase", xml).text() 
     ); 

     $(".content").html(
      $("content", xml).text() 
     ); 
    }); 

XMLを表示されたページにコンテンツを取り除かずに挿入する方法はありますか?

答えて

2

あなたはCDATAブロックでコンテンツをラップする必要があります。このようにして.text()を使用すると、HTMLタグで全コンテンツを取得できます。

<?xml version="1.0" encoding="utf-8"?> 
<tinglePage> 
    <title>Home</title> 
    <authPhrase><![CDATA[welcome, Jashank<br /><a href="/tingle/auth/logout">logout</a>]]></authPhrase> 
    <content> 
    <![CDATA[<p>There are <strong>two</strong> types of folks who sit around 
    thinking about how to kill people: <em>psychopaths</em> and 
    <em>mystery writers</em>. I'm the kind who pays better.</p>]]> 
    </content> 
</tinglePage> 

Working example

0

.text()メソッドは、htmlタグ間の文字(保存スペースとhtmlエンティティ)のみを返します。 .html()メソッドは追加の処理を行わずに文字を返します。私の記憶が私に正しく役立つ場合:

+0

jQueryのドキュメントによると、 '.html()'メソッドは "XML文書では利用できません"と書かれています。私は試しましたが、存在しない 'this.innerHTML'に関するエラーメッセージを受け取ります。 – Jashank

+0

ファッジ、よく私はあなたがそのハハを試してみるべきだったはずです! – lol

関連する問題