jQuery jquery选择这个+类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17745927/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jquery select this + class
提问by zurfyx
How can I select a class from that object this
?
如何从该对象中选择一个类this
?
$(".class").click(function(){
$("this .subclass").css("visibility","visible");
})
I want to select a $(this+".subclass")
. How can I do this with Jquery?
我想选择一个$(this+".subclass")
. 我怎样才能用 Jquery 做到这一点?
回答by Adil
Use $(this).find()
, or pass this in context, using jQuery context with selector.
使用$(this).find()
,或在上下文中传递它,使用带有选择器的jQuery上下文。
Using $(this).find()
使用 $(this).find()
$(".class").click(function(){
$(this).find(".subclass").css("visibility","visible");
});
Using this
in context, $( selector, context )
, it will internally call find function, so better to use find on first place.
this
在上下文中使用$( selector, context )
,它会在内部调用 find 函数,所以最好先使用 find 。
$(".class").click(function(){
$(".subclass", this).css("visibility","visible");
});
回答by Kamil
Maybe something like:
$(".subclass", this);
也许是这样的:
$(".subclass", this);
回答by Gabriel Comeau
What you are looking for is this:
你要找的是这个:
$(".subclass", this).css("visibility","visible");
Add the this
after the class $(".subclass", this)
添加this
课后$(".subclass", this)
回答by Matricore
if you need a performance trick use below:
如果您需要以下性能技巧:
$(".yourclass", this);
find() method makes a search everytime in selector.
find() 方法每次在选择器中进行搜索。
回答by Sandeep Gantait
Well using findis the best option here
在这里使用find是最好的选择
just simply use like this
只是简单地像这样使用
$(".class").click(function(){
$("this").find('.subclass').css("visibility","visible");
})
and if there are many classes with the same name class its always better to give the class name of parent class like this
如果有许多类同名类,最好像这样给出父类的类名
$(".parent .class").click(function(){
$("this").find('.subclass').css("visibility","visible");
})