开发者

Without using document.write how can I insert a DOM node where the current JS is executing?

<body>
  <div id="outer">
    <script>var e = document.createElement("div");
            e.id = "inner1";
            document.body.appendChild(e);</script>
    <script>document.write("<div id='inner开发者_StackOverflow2'></div>");</script>

The structure I want would be: html>body>div#outer>div#inner1+div#inner2

the structure I get is: html>body>(div#outer>div#inner2)+div#inner1


This is terrible beyond my ability to describe, but appears to work for your given situation (I can't tell if you want inner1 and inner2 as children or siblings of outer. this arranges them as siblings).

<body>
<div id="outer">          
    <script>
        var e = document.createElement("div");
        e.id = "inner1";
        document.body.appendChild(e);
    </script>
    <script>
        var scr = '<script>';
        scr += "document.write(\"</div><div id='inner2'>\"); ";
        scr += '<' + '/script>';
        document.write(scr); 
    </script>

the closing </script> string is divided to keep the parser from imploding.

how about this?

<script>
    // document.write("<div id='inner2'></div>");
    var inner2 = document.createElement('div');
    inner2.id = 'inner2';
    //document.getElementById('outer').appendChild(inner2); //as a child of outer
    document.body.appendChild(inner2); // as a sibling of outer
</script>


Look into the jquery documentation. Jquery is a javascript library that you include in your page header. It provides a ton of useful methods for working with the DOM. jQuery is my first choice for writing javascript these days. Straight up js just feels old school to me now. Knowing how to use jQuery effectively (or at least some js library) is a skill every web developer should have. jQuery provides methods like $('css-selector-here').append('what you want to insert'), .prepend(), .insertBefore(), insertAfter(), and .html(), among many others, one of which would probably suit your needs.

Here is a list of all the DOM manipulatuion methods: http://api.jquery.com/category/manipulation/


Can I clarify your question?

You are getting the following:

<html>
    <body>
        <div id='outer'>
            <div id='inner2'></div>
        </div>
        <div id='inner1'></div>
    </body>
</html>

But what you want is:

<html>
    <body>
        <div id='outer'>
            <div id='inner2'></div>
            <div id='inner1'></div>
        </div>
    </body>
</html>

As long as your div#outer is already defined, you can do the following using jQuery. Quick and easy copy & paste. Please give it a shot!

//If you don't already have jQuery, load it from CDN
     <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>   
<script type="text/javascript">
    $(document).ready(function() { //Executes after DOM is ready
       $("#outer")
           .append("<div id='inner1'>INNER DIV 1</div>")
           .append("<div id='inner2'>INNER DIV 2</div>");
    });
    </script>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜