Inner packaging of the prototype

I have searched the documentation and tried various methods, but did not find a suitable technique to achieve the following dom manipulation:

<h3>hello</h3> 

      

to

<h3><a href="#">hello</a></h3>

      

Thank.

+3


source to share


2 answers


As far as I know, prototype has no jQuery equivalent wrapInner()

. You can, however, simulate it using innerHTML :



$$("h3").each(function(element) {
    element.replace("<h3><a href='#'>" + element.innerHTML + "</a></h3>");
});

      

+2


source


The prototype wrap

does work on text nodes, but it doesn't say it clearly and doesn't expand text nodes in the usual way. Also, the selector $$

excludes inline text nodes, so you'll need to load them yourself.



var h3 = $$('h3').first(),
    text = h3.childNodes.first();
Element.wrap(text, 'a', { href: '#' });

      

+1


source







All Articles