css selectors - Use JQuery to wrap multiple sets of elements in a Div -
I'm working on a drop-down menu and changing existing HTML markup with JQuery to enable it to be designed. Will happen. / P>
Here is a simplified example: Wrap all "ul", in which there can be more than one "li" in a div (no div for each UL in the same div).
& lt; Ul & gt; & Lt; Li & gt; Foo & lt; / Li & gt; & Lt; / Ul & gt; & Lt; Ul & gt; & Lt; Li & gt; Foo & lt; / Li & gt; & Lt; Li & gt; Foo & lt; / Li & gt; & Lt; / Ul & gt; & Lt; Ul & gt; & Lt; Li & gt; Foo & lt; / Li & gt; & Lt; Li & gt; Foo & lt; / Li & gt; & Lt; / Ul & gt; & Lt; Script src = "http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" & gt; & Lt; / Script & gt; & Lt; Script & gt; My_selection = []; I = 0; // There is more than one Li ("ul") which is near all ul. Each (function () (if ($ (this) .find ("li"). Length & gt; 1) {// Add it to my_selection My_selection [i] = $ (this); i ++;} // If}); // every // your tag my_selection div tag $ (my_selection). WrapAll (document.createElement ("div")); & Lt; / Script & gt;
The above code gives this firebug error:
"node" can not be inserted at the point specified in "code:" 3 " >
How can I work it?
It would be very clean approach (
$ ('ul: is (li)'). Rap (' gt; & lt; / div & gt;'); First of all, you do not need to create nodes, just provide the wrapping string in jQuery. Second, in your example, every
In order to reduce the efficiency, use the :
selector in this example. Finally, (this will depend on your desired functionality), instead of wrapAll
wrap < / Code>.
Edit: Another option is to use it is the other way around Get
tag And & lt; ul & gt;
parents:
$ ('li: not (only: child)' ..) Parents () ('Ul & lt') wrap (. '; Div & gt; & Lt; / Div & gt; ');
Comments
Post a Comment