Javascript 在$(此)选择器之后选择特殊选择器

Javascript 在$(此)选择器之后选择特殊选择器,javascript,jquery,html,jquery-selectors,Javascript,Jquery,Html,Jquery Selectors,例如,我有这个代码 <script> $(document).ready(function () { $('span').each(function () { $(this).html('<div></div>') ; if ( $(this).attr('id') == 'W0' ) { $( this > div ?!!! ).text('0') } if ( $(

例如,我有这个代码

<script>

$(document).ready(function () {     
    $('span').each(function () {    

        $(this).html('<div></div>') ;    
        if ( $(this).attr('id') == 'W0' ) { $( this > div ?!!! ).text('0') }
        if ( $(this).attr('id') == 'W1' ) { $( this > div ?!!! ).text('1') }
        if ( $(this).attr('id') == 'W2' ) { $( this > div ?!!! ).text('2') }

    });     
});

</script>

<span id="W0"></span>
<span id="W1"></span>
<span id="W2"></span>

$(文档).ready(函数(){
$('span')。每个(函数(){
$(this.html(“”);
if($(this).attr('id')=='W0'){$(this>div?!!!).text('0')}
if($(this.attr('id')=='W1'){$(this>div?!!!).text('1')}
if($(this).attr('id')=='W2'){$(this>div?!!!).text('2')}
});     
});
但是
$(this>div)
$(this>div')
是错误的选择器&不起作用


你们建议我做什么

您可以按如下方式使用它:

$(' > div', $(this))
文件:

对于直接子元素,可以使用
子元素

$(this).children('div')
文件:

使用
find

$(this).find(' > div')
文件:

您可以将a与选择器一起传递

$(' > div ', this )
或者像这样使用


但您的解决方案可以按如下方式完成

$(文档).ready(函数(){
变量文本={
W0:'0',
W1:‘1’,
W2:'2'
}
$('span')。每个(函数(){
$('', {
text:text[this.id]
}).附于(本)
});
});

$(this).children('div')