javascript - jQuery append DOM - Stack Overflow

All the examples of jQuery.append() seem to take an html string and append it to a container. I have a

All the examples of jQuery.append() seem to take an html string and append it to a container. I have a slightly different use case. My server returns me an XML that contains HTML text to be displayed, something like:

<event source="foo">
    <contents>
        <h1>This is an event</h1>
        This is the body of the event
    </contents>
</event>

I have a div where this content needs to be displayed.

My JS currently does the following:

  1. Loads up the XML data into jQuery in the $.ajax() success handler:

    var jData = $( data );

  2. 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 ) );
        });
    }
    

The append() call fails with Uncaught Error: WRONG_DOCUMENT_ERR: DOM Exception 4 on Chrome. The debugger shows value to be a DOM Element object and $( value ) to be an Object that contains the Element.

Any help will be appreciated.

Thanks. -Raj

All the examples of jQuery.append() seem to take an html string and append it to a container. I have a slightly different use case. My server returns me an XML that contains HTML text to be displayed, something like:

<event source="foo">
    <contents>
        <h1>This is an event</h1>
        This is the body of the event
    </contents>
</event>

I have a div where this content needs to be displayed.

My JS currently does the following:

  1. Loads up the XML data into jQuery in the $.ajax() success handler:

    var jData = $( data );

  2. 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 ) );
        });
    }
    

The append() call fails with Uncaught Error: WRONG_DOCUMENT_ERR: DOM Exception 4 on Chrome. The debugger shows value to be a DOM Element object and $( value ) to be an Object that contains the Element.

Any help will be appreciated.

Thanks. -Raj

Share Improve this question asked Sep 9, 2010 at 17:44 RajRaj 2,9204 gold badges30 silver badges49 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

You can't append nodes that belong to one DOM tree to another document.

Try to clone them:

$("#eventDiv").append( jData.find("contents").children().clone() );

or simply use their textual representation to have them re-created:

$("#eventDiv").append( jData.find("contents").html() );

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744306028a4567715.html

相关推荐

  • javascript - jQuery append DOM - Stack Overflow

    All the examples of jQuery.append() seem to take an html string and append it to a container. I have a

    7天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信