简体   繁体   English

使用jQuery从标签中删除类时遇到问题

[英]Problems removing a class from tag with jquery

I have few div box that looks like this, they all are like productholder2 , productholder3 etc. This is one of them: 我有几个看起来像这样的div框,它们都像productholder2productholder3等。这是其中之一:

 <div class="productholder1>  
    <p class="hidden">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod   tempor incididunt</p>     
    <input type="button" value="Läs mer" class="button">                                               
</div>

What I want to achieve is to remove this hidden class on the <p> tag by using jQuery. 我要实现的是使用jQuery在<p>标记上删除此隐藏的类。

Somehow I need to specify that it's the productholder1 's <p> class I want to remove so the text pops up since in the CSS I have .hidden{display:none); 我需要以某种方式指定它是我要删除的productholder1<p>类,因此会弹出文本,因为在CSS中,我有.hidden{display:none); by removing the class the text should pop up. 通过删除该类,应该弹出文本。

I have tried this following jQuery that I made but it doesn't work. 我已经尝试过以下我制作的jQuery,但无法正常工作。 What am I doing wrong? 我究竟做错了什么?

<script type="text/javascript">
   $(document).ready(function () {
       $(".button").click(function () {
           $('.productholder1').removeClass("hidden");
       });
   });
</script>

The hidden class is is on the <p> element not the <div> element which is what you're currently selecting, so you would want to use a selector of '.productholder1 p' to grab it and remove the class. hidden类位于<p>元素上,而不是当前正在选择的<div>元素上,因此您需要使用'.productholder1 p'选择器来抓取它并删除该类。 If you only want <p> elements immediately below the product holder div, you can use this selector '.productholder1 > p' as well. 如果只想在产品支架div的下方放置<p>元素,则也可以使用此选择器'.productholder1 > p'

<script type="text/javascript">
   $(document).ready(function () {
       $(".button").click(function () {
           $('.productholder1 p').removeClass("hidden");
       });
   });
</script>

Some other options - 其他一些选择-

  1. All <p> elements with a hidden class immediately below a <div> 所有<p>元素都在<div>正下方有一个hidden
    1. $('div > p.hidden')
  2. Multiple selectors with a <p> below 下方带有<p>多个选择器
    1. $('.productholder1 p, .productholder2 p, .productholder3 p')

The hidden class is on a child of .productholder1 , not on the element itself. hidden类位于.productholder1级上,而不位于元素本身上。 It's also a sibling of the button. 这也是按钮的同级

This line will remove that class from any (descendent) element of .productholder1 that has that class. 该行将从具有该类的.productholder1任何(后代)元素中删除该类。 For efficiency, it'll completely ignore any element that isn't already hidden: 为了提高效率,它将完全忽略任何尚未隐藏的元素:

$('.productholder1 .hidden').removeClass('hidden');

However - I think your plan was that each button only affects the elements that are in the same group? 但是 -我认为您的计划是每个按钮仅影响同一组中的元素? If so, use this instead, which relies on just traversing the DOM hierarchy rather than there being a particular class on each group. 如果是这样,请改用此方法,它依赖于仅遍历DOM层次结构,而不是每个组上都有特定的类。

$(".button").click(function () {
    $(this).parent().find('.hidden').removeClass('hidden');
});

This one chunk of code will then work for every group of elements, without you having to duplicate it for each class .productholderNNN . 那么这个代码一个大块将为每个组元素的工作,而您不必重复它为每个类.productholderNNN

FWIW, using numbered classes to represent the individual members of groups of otherwise identical DOM structures is actually a mis-use of CSS classes. FWIW使用编号的类表示本来是相同的DOM结构的组中的各个成员,实际上是对CSS类的误用。 That's what IDs are for. 这就是ID的用途。 Classes are supposed to represent groups of elements that should be treated identically. 类应该代表应该被同等对待的元素组。

its should like 它应该喜欢

  $('.productholder1').find("p").removeClass("hidden");

or 要么

$('.productholder1 p').removeClass("hidden");

what you are trying to do is removing the class in .productholder1 but the classs hidden is not there so you need to select the .productholder1 to grab it 您想要做的是删除.productholder1的类,但是隐藏的类不存在,因此您需要选择.productholder1来抓取它。

Try this code: 试试这个代码:

   <script type="text/javascript">
           $(document).ready(function () {
               $(".button").click(function () {
                   $('.productholder1 p').removeClass("hidden");
               });
           });
        </script>

or 要么

 <script type="text/javascript">
               $(document).ready(function () {
                   $(".button").click(function () {
                       $('.productholder1').find('p').removeClass("hidden");
                   });
               });
            </script>

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

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