¿Por qué jquery wrap está ejecutando un javascript en línea?

I tried to wrap an element with inline javascript and it is getting execute again...here is the jsFiddle


preguntado el 01 de febrero de 12 a las 22:02

1 Respuestas

Because when JQuery wraps a DOM element, it removes it, and re-inserts it into the DOM as a child element of the new wrapper element. If it contains inline JS, it'll get re-executed. That's just how it works. That's why you should keep your JS out of the DOM :)

A simple hack work-around if you MUST have your JS in that script block is to create a flag listener/setter, like so:

 <script type='text/javascript'>
     if(window.helloworld !== true){
         window.helloworld = true;


Of course I wouldn't recommend creating window properties like that in your production application, but the proof of concept is there, do with it what you will.

Respondido 02 Feb 12, 02:02

what is the solution to just wrap it - coool

I think I am going to ahead with direct dom manipulation and have $.fn.wrap for it... jsfiddle.net/uk67F/3 - coool

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.