简体   繁体   English

如何在jQuery中使用jquery变量?

[英]How to use jquery variable in jQuery?

I have a div in which there is criterion to apply different class. 我有一个div,其中有适用不同课程的标准。 For example if in loop i is odd then .odd-product is applied to the div and if i is even then .even-product is applied to the div. 例如,如果循环中i为奇数,则将.odd-product应用于div,如果i为偶数,则将.even-product应用于div。

Here it is: 这里是:

<div id="product"<?php if($i%2==0){?>class="even-product"<?php } else {?>class="odd-product"<?php } ?>>

Now I reference that div in the following way in jQuery: 现在,我在jQuery中以以下方式引用该div:

    $divClass=$('#product').attr("class");

Now I want to hide a div whose class name is contained in $divClass variable. 现在,我想隐藏其类名称包含在$ divClass变量中的div。 How to do that? 怎么做?

You would just use it like a string: 您只需要像字符串一样使用它:

  $divClass=$('#product').attr("class");
  $("." + $divClass).hide();

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

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