title title<...">

JQuery efficiency question

I have HTML like this:

<div id="best">
    <img src="image.jpg">
    <span>title</span>
    <img src="image.jpg">
    <span>title</span>
    <img src="image.jpg">
    <span>title</span>
</div> 

      

I want the jQuery code to strip all gaps. What's better:

$('#best').find('span').remove();

      

or

$('#best').children('span').remove();

      

or

$('#best').find('span').each().remove();

      

or is there a better solution? What's better?

+2


a source to share


3 answers


$('#best span').remove();

      



+4


a source


Go for readability:



$('#best > span').remove()

      

0


a source


1 and 3 are identical. 2 differs in that it only removes direct children #best

, while the other two will remove children at any level. It really depends on you whether you use find

or children

, as it depends on your intentions, but there is no need for each

.

You can, however, contract it all in the selector, so that 1 becomes $('#best span').remove();

and 2 becomes $('#best > span').remove();

.

0


a source







All Articles