I have ASP.Net code similar to the following (this is inside a FIELDSET):
<... more of the same...>
...
I'm trying to keep my markup as clean as I possibly can, but I've decided that for various reasons, I need to wrap a DIV around everything in the list item after the first label, like this:
<... more of the same...>
...
I would rather do this with "unobtrusive Javascript" via jQuery instead of littering my page with extra markup so I can keep the form semantically "clean".
I know how to write a jQuery selector to get to the first label in each list item $("li+label") or use :first-child. I also know how to insert things after the selection.
What I can't figure out (at least this late at night) is how to find everything after the first label in the list item (or basically everything in the list item except for the first label would be another way to put it) and wrap a DIV around that in the document ready function.
**UPDATE:**
Owen's code worked once I removed the single quotes from around:
$('this')
and set the proper decendent selector:
$("li label:first-child")
in order to only select the first label that occurs after a list item.
Here is what I did:
$(document).ready(function() {
$('li label:first-child').each(function() {
$(this).siblings().wrapAll('');
});
});
以上就是How do I use jQuery to insert a
wrapped around a variable number of child elements?的详细内容,更多请关注web前端其它相关文章!