所有关于jQuery.append()的例子似乎都是将HTML字符串附加到容器中。我有一个稍微不同的用例。我的服务器会返回一个包含要显示的HTML文本的XML,类似于:
我有一个 div,需要在其中显示这个内容。
我的 JS 目前执行以下操作:
任何帮助将不胜感激。
谢谢。 -Raj
<event source="foo">
<contents>
<h1>This is an event</h1>
This is the body of the event
</contents>
</event>
我有一个 div,需要在其中显示这个内容。
我的 JS 目前执行以下操作:
Loads up the XML data into jQuery in the $.ajax() success handler:
var jData = $( data );
Find the contents tag and tries to add its children to the div that is supposed to display the event:
var contents = jData.find( "contents" ); if( contents != null ) { $( contents ).children().each( function( index, value ) { $( "#eventDiv" ).append( $( value ) ); }); }
任何帮助将不胜感激。
谢谢。 -Raj
.html()
失败是因为你在处理一个 XML 文档,而 jQuery 并不完全适用于处理所有情况下的 XML。 - Tomalak<contents>
中对所有内容进行XML编码,并执行$("#eventDiv").append( jData.find("contents").text() );
。 - Tomalak