在jquery中选择html元素对

boz*_*boz 6 html jquery

我正在使用一些生成html的服务器代码,它看起来像:

<input type="radio" />
<label>something</label>

<input type="radio" />
<label>something</label>

<input type="radio" />
<label>something</label>

<input type="radio" />
<label>something</label>
Run Code Online (Sandbox Code Playgroud)

我想在一个范围内包装每一对但我无法找到一种方法来选择jquery上的元素对以便wrapAll()在它们上使用.我无法更改我正在使用的html.有人可以帮忙吗?

bjo*_*rnd 11

$('input').each(function(){
    $(this).next('label').add(this).wrapAll('<span>');
});
Run Code Online (Sandbox Code Playgroud)
  1. next 会找到最近的兄弟元素.
  2. add 将匹配的项添加到集合中.

演示