简体   繁体   English

如何使用jquery为'this'类更改css样式?

[英]How can I change a css style for 'this' class using jquery?

I have a list of images that each are tied to a class. 我有一个图像列表,每个图像都绑定到一个类。

var hoverList = this.hoverable.getAllList() //this gets the list.

on mouseover of the images, I want a block of text in a different area, but shares a class, to display. 在鼠标悬停的图像上,我想要一个不同区域的文本块,但共享一个类,以显示。 I call 我打电话

hoverList.mouseover(this.hoverable.displayTheDeets)

and it runs 它运行

displayTheDeets: function(){
    big=$(".project-details")
    thisClass=$(this).attr("class")
    console.log(thisClass)
    //$(big).find(thisClass).css("display","")
    $(big).find(thisClass).css("display", "block")
    //$(big > thisClass).css("display","block")
}

From the console, if I run the literal command 从控制台,如果我运行文字命令

$(".project-details").find(".code-fusion")

it returns the element I want. 它返回我想要的元素。 And I can change the display with no problem. 我可以毫无问题地更改显示。

I think my problem is with thisClass. 我认为我的问题在于thisClass。 Any thoughts would be appreciated. 任何想法将不胜感激。

Try, 尝试,

big.find('.' + thisClass).css("display", "block");

or simply 或者干脆

big.find('.' + thisClass).show();

Please note that .attr('class') would return the class being used for the particular element, and it wont return the class name in the format of selector. 请注意.attr('class')将返回用于特定元素的类,并且它不会以selector的格式返回类名。

Additional Note : And in sometimes if you have more than one class set with that particular element then it would make the selector as invalid like $('.class1 class2 class3') 附加说明 :有时如果你有多个具有该特定元素的类集,那么它会使选择器$('.class1 class2 class3')无效,如$('.class1 class2 class3')

Try with prefix dot. 尝试使用前缀dot。 the attr("class") returns only the name not the prefix dot. attr(“class”)只返回名称而不是前缀dot。

$(big).find("."+ thisClass).css("display", "block")

or 要么

 $(big).find("."+ thisClass).show();

尝试

$(big).find("."+ thisClass).css("display", "block")

Part of the problem has already been answered well enough; 部分问题已经得到了很好的回答; however, I would suggest that you solve this problem in another way. 但是,我建议你以另一种方式解决这个问题。

You can define the class name separately from the class of your hovering element, eg 您可以与悬停元素的类别分别定义类名,例如

<... class="something" data-linked="theclass">

Then: 然后:

displayTheDeets: function(){
    var className = $(this).data('linked');

    $(".project-details").find('.' + className).show();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM