Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有这个:
... <input type="checkbox" /> AAA <p>BBB</p> <div>CCC</div> ...
并希望通过 LABEL 标签包装输入和下一个文本,如下所示:
... <label> <input type="checkbox" /> AAA </label> <p>BBB</p> <div>CCC</div> ...
我怎么能通过 jQuery 做到这一点?
$('.parent').contents().filter(function(){ return this.localName === 'input' || this.nodeType === 3; }).wrapAll('<label></label>');
http://jsfiddle.net/Cja4H/
或者:
$('input[type=checkbox]').each(function(){ $(this).add(this.nextSibling).wrapAll('<label/>'); });
http://jsfiddle.net/BkpgX/