简体   繁体   中英

Insert just opening HTML tags after an element?

I would like to insert a couple of opening DIV tags after the H1 element on a page, without inserting the corresponding closing tags (since the closing tags are contained in an included footer file which I don't have access to).

ie Existing code:

<body>
<h1>Heading One</h1>
... page content...
</div>
</div>
</body>

New code:

<body>    
<h1>Heading One</h1>
<div id="foo">  
<div id="baa">  
... page content...
</div>
</div>
</body>

DOM methods insert the div as a complete (closed) element, 'createTextNode' inserts escaped characters and 'innerHTML' needs an element to insert into. Have even tried to insert a script element with document.write without any luck.

Any ideas (jQuery would be fine)?

Update The following worked:

document.body.innerHTML = document.body.innerHTML.replace('</h1>','</h1><div id="foo"><div id="baa">')

As pointed out by Asad the solution (which now seems obvious of course) is to use string methods on the HTML rather than DOM methods.

If you're dealing with DOM manipulation, use DOM manipulation methods. If you're dealing with HTML manipulation, use string manipulation methods.

h1.parentElement.innerHTML = h1.parentElement.innerHTML.replace("<h1>Heading One</h1>","<h1>Heading One</h1><div><div>");

i think this will answer your question, it is all about valid XML formation.

http://www.w3schools.com/xml/xml_syntax.asp

忘记DOM方法,使用.replace()将其作为字符串插入。

Your approach is fundamentally wrong. The browser parses the DOM as it sees it, and automatically closes any tags that ought to be closed. It's impossible to use JavaScript to insert only the opening tag.

You say "the closing tags are contained in an included footer file which I don't have access to." Closed tags that haven't been opened are ignored, so as far as the DOM parser is concerned, those closing tags don't exist.

Your solution is either:

  1. Put the opening tags in a header, or somewhere else on the server-side, or
  2. Use JavaScript to grab ALL the following DOM elements, including the footer, and .wrap() them all in the desired divs.

This kind of practice seems a bit unorthodox, but perhaps something like this would help.

Existing HTML

<h1 id="testH1">Test H1</h1>
<div id="existingDiv">
    <div id="existingDivContent">Existing Div Content</div>
</div>

New HTML

<h1 id="testH1">Test H1</h1>
<div id="newDiv">
    <div id="existingDiv">
        <div id="existingDivContent">Existing Div Content</div>
    </div>
</div>

JS

The javascript is fairly rudimentary, but I think the concept can be applied to safely and properly achieve your goal.

$(document).ready(function() {
    //-- parent node you wish to copy
    var existingDiv = $('#existingDiv');

    //-- new parent div node
    var newDiv = $('<div id="newDiv">');

    //-- where we want to insert the new parent node
    var testH1 = $('#testH1');

    //-- stuff our previous parent node into our new parent node
    newDiv.html(existingDiv);

    //-- insert into the DOM
    testH1.after(newDiv);

});

http://jsfiddle.net/8qzvN/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM