在$(this)选择器之后选择特殊选择器 [英] select special selector after $(this) selector
本文介绍了在$(this)选择器之后选择特殊选择器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
例如我有这段代码
< script>
$ b $(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/
OR
对于直接子元素,您可以使用 children
:
$(this).children('div')
文档: http:// api .jquery.com / children /
或
使用 find
$(this).find('> div' )
文档: http://api.jquery.com/find/
for example I have this code
<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>
But $( this > div )
or $( this ' > div ' )
are wrong selector & doesn't work
So what do u guys suggest I should do ?
解决方案
You can use it as follow:
$(' > div', $(this))
Docs: http://api.jquery.com/child-selector/
OR
For direct child elements, you can use children
:
$(this).children('div')
Docs: http://api.jquery.com/children/
OR
Using find
$(this).find(' > div')
Docs: http://api.jquery.com/find/
这篇关于在$(this)选择器之后选择特殊选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文