How do I use jQuery to insert a
wrapped around a variable number of child elements?

I have ASP.Net code similar to the following (this is inside a FIELDSET):
  1. <... more of the same...>
  2. ...
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:
  1. <... more of the same...>
  2. ...
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前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » jQuery 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准

联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏