例如我有这个代码
<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')
关于javascript - 在 $(this) 选择器之后选择特殊选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31366401/