javascript - 在 $(this) 选择器之后选择特殊选择器

标签 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 ( $(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>

但是 $( this > div )$( this ' > div ' ) 是错误的选择器并且不起作用

那么你们建议我应该做什么?

最佳答案

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

$(' > div', $(this))

文档:http://api.jquery.com/child-selector/

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

$(this).children('div')

文档:http://api.jquery.com/children/

使用查找

$(this).find(' > div')

文档:http://api.jquery.com/find/

Demo

关于javascript - 在 $(this) 选择器之后选择特殊选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31366401/

相关文章:

php - 使用ajax函数和php删除行

javascript - HTML 中 Javascript 函数的问题

javascript - 自动日历 php html

javascript - 将变量传递给jquery上传函数

javascript - 如何更改选择的默认行为并仅显示所选元素?

javascript - 设置默认颜色而不弄乱页面

html - Chrome 存储新密码但不将其链接到当前用户名 : how can I fix it?

javascript - 如何将 lazyloader 与使用背景图像的猫头鹰轮播一起使用

javascript - 在 Node.js 中修改大量 json 文件/数组时的奇怪之处

javascript - 在同一行中靠近它的 td 标签内查找隐藏的输入