可以使用$(this)和通用选择器(*)
Is possible to use $(this) and universal selector (*)?
我想知道是否可以将通用选择器与$(this)
一起使用。例如,当我想从元素及其子元素中删除所有内联CSS时,我使用以下代码:
$('#element, #element *').attr('style','');
但如果我有$(this)
,代码是什么?
也许我得试试:
$(this, this+'*').attr('style','');
您可以使用find()
$(this).find('*').attr('style','');
或上下文选择器
$('*', this).attr('style','');
用this
做与$('#element *')
相同的事情
如果this
表示的元素应该是集合的一部分,则可以将其添加回
$(this).find('*').addBack().attr('style','');
我倾向于同意Rory McCrossan在评论中的观点,使用父元素和外部样式比更改多个元素的样式属性更可取
.element.styled * { /* styles for all children */ }
然后只是
$('.element').removeClass('styled')
删除子上的样式
您可以找到孩子,然后使用removeAttr
$(this).children().removeAttr('style');
或
$(this).find('*').removeAttr('style');
jQuery查找方法将起作用:
$(this).find(selector);
您可以使用jQuery的.children()
:
$(this).children().attr('style','');
然而,这只会降低一个级别,所以您也可以使用.find()
:
$(this).find('*').attr('style','');
看看https://jsfiddle.net/joe_young/zofhwca6/
您可以使用这个:
$(this).find('*').removeAttr('style');
甚至上下文:
$('*', this).removeAttr('style');
$(this).children()
可用于选择其子级此外,您可以像$(this).children('.classname')
一样进行特定
类似地,$(this).parent()
可以用于选择其父
相关文章:
- 使用THIS比使用选择器更有效吗
- 将jQuery选择器与“this”相结合
- 正在获取$this的选择器
- 在Jquery中将$(this)与css类选择器一起使用
- Jquery $this属性选择器是否包含单词
- 在新对象参数中使用“this”选择器
- 如何使用“this”作为选择器的一部分
- 如何在 jQuery 中使用“this”的子元素作为选择器
- jQuery $(this) 伪选择器
- 如何在 JQuery 中使用带有“this”的相邻同级选择器
- jQuery两个不同的选择器,但根据哪一个定义$(this)
- 在类选择器中传递 jQuery this.id
- 当有多个选择器时处理 $(this)
- 在 Jquery 中使用选择器而不是“this”
- javascript “this” 如何处理来自 jquery 的多个选择器返回
- 可以使用$(this)和通用选择器(*)
- 我如何在香草javascript中:选择器,事件和$(this)的需要
- 为什么$(this)选择器在我的函数内部工作,而在外部工作
- 我如何将包含$(this)的函数应用于选择器的范围
- jQuery 选择器 - “this”或“data”