简体   繁体   English

jQuery中是否存在更深层次的条件,用于将类的CSS值分配给变量? 它不适用于我想要的所有课程

[英]Are there deeper conditions in jquery for assigning css values of classes to variables? It doesn't work with all classes I would like

It often works to assign as mentioned in title, for example: 如标题中所述,通常可以进行分配,例如:

var a1 = $('.entry--menu .btn.entry--link').css('left');

When I execute alert(a1) it shows me the assigned value of the property left of the class .entry--menu .btn.entry--link 当我执行alert(a1)它向我显示了类.entry--menu .btn.entry--link left属性的赋值

But there are other cases where it doesn't work, for example: 但是在其他情况下,它不起作用,例如:

var a2 = $('.last-seen-products .product--box .product--image .image--element').css('width');

When I execute alert(a2) , it shows null . 当我执行alert(a2) ,它显示为null

I tried different things in order to find out why - for example I assigned in the css file at the top the following: width:100px But nothing works. 我尝试了不同的方法以找出原因-例如,我在顶部的css文件中分配了以下内容: width:100px但没有任何效果。

Is there something that I overlook? 有什么我可以忽略的吗? Are there requirements, for example something like (a fictitious) requirement that the class must have the property position:absolute or something similar?? 是否存在一些要求,例如(虚构的)要求,该类必​​须具有属性position:absolute或类似的东西?

it "works" for me if you separate the classes by "," 如果您将班级之间用“,”分开,则对我“有效”

var a2 = $('.last-seen-products, .product--box, .product--image, .image--element').css('width');

but it returns the last value it founds in the last class where it founds it... I mean if: 但是它返回在上一个类中找到的最后一个值...我的意思是:

.last-seen-products{ width: 100px; }
.image--element{ width: 10px }
.product--image{ width: 500px }

it will return "10px" because is the last class in the list... hope it helps. 它将返回“ 10px”,因为它是列表中的最后一个类。希望对您有所帮助。

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

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