...">

How to use jQuery replaceWith to replace varying number elements

I have a varying number of elements with the same class name:

I want to replace each of these with a new set of elements with the same class name, but there could be more or fewer elements:

or

If I use `$('p.test').replaceWith('new content')`, it will replace each element with the content so I can't do that. What is the best way to accomplish this? EDIT (another explanation): So I have these elements on the page:

Then, I dynamically retrieve a new set of elements (a varying number of them) that have the same class name (and maybe some other class names). For example:

In the above example, I want the first three `

` elements to be gone. And in their place should be the 2 dynamically retreived `

` elements. Keep in mind the number of elements of either set can vary.
Why do they have the same class? If you want each to have different elements you should use different id's. Or append new elements on the fly with jquery. I dont quite understand your question, could you please explain a little better? Thanks :)

以上就是How to use jQuery replaceWith to replace varying number elements的详细内容,更多请关注web前端其它相关文章!

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

评论 抢沙发

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

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

联系我们

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

支付宝扫一扫打赏

微信扫一扫打赏