javascript - 如何在多个地方使用toggle Class功能?

标签 javascript jquery html css toggleclass

我不知道如何在单击向下箭头时显示整个文本。这是我尝试过的方法,它只适用于一个文本框,但是当我制作 2 个或更多文本框时,它不起作用?

有效的方法

body{
    background-color: dimgray;
}
.box{
    padding: 10px;
    border-bottom: 0.1px solid #dadada;
    width: 700px;
    background-color: #fff;
}
.box-content{
    height: 30px;
    overflow: hidden;
    transition: 0.3s;
}
.box-expand{
    height: auto !important;    
}
    
.box-opt{
    float: right;
}
.box-opt a{
    text-decoration: none;
    font-size: 70%;
    color: #646464;
    display: inline-block;
    padding: 5px;
    
<script src="https://use.fontawesome.com/e74aa8f5bb.js"></script>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>


<div class="box">
   
   <div class="box-opt">
        <a href="#" id="box-toggle"> <i class="fa fa-chevron-down" aria-hidden="true"></i> </a>
    </div>
    
    <p class="box-content" id="box-content">this is an awsome comment bout this post this is an awsome comment bout this posttis is 7 this is an awsome comment bout this post this is an awsome comment bout this posttis is 7 this is an awsome comment bout this post this is an awsome comment bout this posttis is 7 this is an awsome comment bout this post this is an awsome comment bout this posttis is 7 this is an awsome comment bout this post this is an awsome comment bout this posttis is 7 this is an awsome comment bout this post this is an awsome comment bout this posttis is 7 this is an awsome comment bout this post this is an awsome comment bout this posttis is 7 </p>         
                    
    <script>
        $("#box-toggle").click(function (e) {
            e.preventDefault();
            $(".box-content").toggleClass("box-expand");
        });
    </script>

它只适用于一个文本框,但是当我制作 2 个或更多文本框时,它不起作用?

一个不做兽人

body{
    background-color: dimgray;
}
.box{
    padding: 10px;
    border-bottom: 0.1px solid #dadada;
    width: 700px;
    background-color: #fff;
  margin: 10px;
}
.box-content{
    height: 30px;
    overflow: hidden;
    transition: 0.3s;
}
.box-expand{
    height: auto !important;    
}
    
.box-opt{
    float: right;
}
.box-opt a{
    text-decoration: none;
    font-size: 70%;
    color: #646464;
    display: inline-block;
    padding: 5px;
    
<script src="https://use.fontawesome.com/e74aa8f5bb.js"></script>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>


<div class="box">
   
   <div class="box-opt">
        <a href="#" id="box-toggle"> <i class="fa fa-chevron-down" aria-hidden="true"></i> </a>
    </div>
    
    <p class="box-content" id="box-content">this is an awsome comment bout this post this is an awsome comment bout this posttis is 7 this is an awsome comment bout this post this is an awsome comment bout this posttis is 7 this is an awsome comment bout this post this is an awsome comment bout this posttis is 7 this is an awsome comment bout this post this is an awsome comment bout this posttis is 7 this is an awsome comment bout this post this is an awsome comment bout this posttis is 7 this is an awsome comment bout this post this is an awsome comment bout this posttis is 7 this is an awsome comment bout this post this is an awsome comment bout this posttis is 7 </p>         
                    
    <script>
        $("#box-toggle").click(function (e) {
            e.preventDefault();
            $(".box-content").toggleClass("box-expand");
        });
    </script>
    
</div><div class="box">
   
   <div class="box-opt">
        <a href="#" id="box-toggle"> <i class="fa fa-chevron-down" aria-hidden="true"></i> </a>
    </div>
    
    <p class="box-content" id="box-content">this is an awsome comment bout this post this is an awsome comment bout this posttis is 7 this is an awsome comment bout this post this is an awsome comment bout this posttis is 7 this is an awsome comment bout this post this is an awsome comment bout this posttis is 7 this is an awsome comment bout this post this is an awsome comment bout this posttis is 7 this is an awsome comment bout this post this is an awsome comment bout this posttis is 7 this is an awsome comment bout this post this is an awsome comment bout this posttis is 7 this is an awsome comment bout this post this is an awsome comment bout this posttis is 7 </p>         
                    
    <script>
        $("#box-toggle").click(function (e) {
            e.preventDefault();
            $(".box-content").toggleClass("box-expand");
        });
    </script>
    
</div><div class="box">
   
   <div class="box-opt">
        <a href="#" id="box-toggle"> <i class="fa fa-chevron-down" aria-hidden="true"></i> </a>
    </div>
    
    <p class="box-content" id="box-content">this is an awsome comment bout this post this is an awsome comment bout this posttis is 7 this is an awsome comment bout this post this is an awsome comment bout this posttis is 7 this is an awsome comment bout this post this is an awsome comment bout this posttis is 7 this is an awsome comment bout this post this is an awsome comment bout this posttis is 7 this is an awsome comment bout this post this is an awsome comment bout this posttis is 7 this is an awsome comment bout this post this is an awsome comment bout this posttis is 7 this is an awsome comment bout this post this is an awsome comment bout this posttis is 7 </p>         
                    
    <script>
        $("#box-toggle").click(function (e) {
            e.preventDefault();
            $(".box-content").toggleClass("box-expand");
        });
    </script>
    
</div><div class="box">
   
   <div class="box-opt">
        <a href="#" id="box-toggle"> <i class="fa fa-chevron-down" aria-hidden="true"></i> </a>
    </div>
    
    <p class="box-content" id="box-content">this is an awsome comment bout this post this is an awsome comment bout this posttis is 7 this is an awsome comment bout this post this is an awsome comment bout this posttis is 7 this is an awsome comment bout this post this is an awsome comment bout this posttis is 7 this is an awsome comment bout this post this is an awsome comment bout this posttis is 7 this is an awsome comment bout this post this is an awsome comment bout this posttis is 7 this is an awsome comment bout this post this is an awsome comment bout this posttis is 7 this is an awsome comment bout this post this is an awsome comment bout this posttis is 7 </p>         
                    
    <script>
        $("#box-toggle").click(function (e) {
            e.preventDefault();
            $(".box-content").toggleClass("box-expand");
        });
    </script>
    
</div>

最佳答案

具有相同 ID 的元素不应超过一个——它们应特定于一个元素。话虽这么说,我会将 box-toggle 更改为一个类而不是 ID,并且还会删除您盒子上的 id="box-content"。您也不需要将脚本放在每个框之后——只需一次即可:

$('.box-toggle').click(function(e){ 
e.preventDefault(); 
$(this).parents('.box').find('.box-content').toggleClass('box-expand');
});

这样您就可以只定位您在其中单击的框。

body{
    background-color: dimgray;
}
.box{
    padding: 10px;
    border-bottom: 0.1px solid #dadada;
    width: 700px;
    background-color: #fff;
  margin: 10px;
}
.box-content{
    height: 30px;
    overflow: hidden;
    transition: 0.3s;
}
.box-expand{
    height: auto !important;    
}
    
.box-opt{
    float: right;
}
.box-opt a{
    text-decoration: none;
    font-size: 70%;
    color: #646464;
    display: inline-block;
    padding: 5px;
    
<script src="https://use.fontawesome.com/e74aa8f5bb.js"></script>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>


<div class="box">
   
   <div class="box-opt">
        <a href="#" class="box-toggle"> <i class="fa fa-chevron-down" aria-hidden="true"></i> </a>
    </div>
    
    <p class="box-content">this is an awsome comment bout this post this is an awsome comment bout this posttis is 7 this is an awsome comment bout this post this is an awsome comment bout this posttis is 7 this is an awsome comment bout this post this is an awsome comment bout this posttis is 7 this is an awsome comment bout this post this is an awsome comment bout this posttis is 7 this is an awsome comment bout this post this is an awsome comment bout this posttis is 7 this is an awsome comment bout this post this is an awsome comment bout this posttis is 7 this is an awsome comment bout this post this is an awsome comment bout this posttis is 7 </p>         
                    
    
</div><div class="box">
   
   <div class="box-opt">
        <a href="#" class="box-toggle"> <i class="fa fa-chevron-down" aria-hidden="true"></i> </a>
    </div>
    
    <p class="box-content">this is an awsome comment bout this post this is an awsome comment bout this posttis is 7 this is an awsome comment bout this post this is an awsome comment bout this posttis is 7 this is an awsome comment bout this post this is an awsome comment bout this posttis is 7 this is an awsome comment bout this post this is an awsome comment bout this posttis is 7 this is an awsome comment bout this post this is an awsome comment bout this posttis is 7 this is an awsome comment bout this post this is an awsome comment bout this posttis is 7 this is an awsome comment bout this post this is an awsome comment bout this posttis is 7 </p>         
                    
    
</div><div class="box">
   
   <div class="box-opt">
        <a href="#" class="box-toggle"> <i class="fa fa-chevron-down" aria-hidden="true"></i> </a>
    </div>
    
    <p class="box-content">this is an awsome comment bout this post this is an awsome comment bout this posttis is 7 this is an awsome comment bout this post this is an awsome comment bout this posttis is 7 this is an awsome comment bout this post this is an awsome comment bout this posttis is 7 this is an awsome comment bout this post this is an awsome comment bout this posttis is 7 this is an awsome comment bout this post this is an awsome comment bout this posttis is 7 this is an awsome comment bout this post this is an awsome comment bout this posttis is 7 this is an awsome comment bout this post this is an awsome comment bout this posttis is 7 </p>         
                    
    
</div><div class="box">
   
   <div class="box-opt">
        <a href="#" class="box-toggle"> <i class="fa fa-chevron-down" aria-hidden="true"></i> </a>
    </div>
    
    <p class="box-content">this is an awsome comment bout this post this is an awsome comment bout this posttis is 7 this is an awsome comment bout this post this is an awsome comment bout this posttis is 7 this is an awsome comment bout this post this is an awsome comment bout this posttis is 7 this is an awsome comment bout this post this is an awsome comment bout this posttis is 7 this is an awsome comment bout this post this is an awsome comment bout this posttis is 7 this is an awsome comment bout this post this is an awsome comment bout this posttis is 7 this is an awsome comment bout this post this is an awsome comment bout this posttis is 7 </p>         
                    
    <script>
        $('.box-toggle').click(function(e){ 
e.preventDefault(); 
$(this).parent().parent().find('.box-content').toggleClass('box-expand');
});
    </script>
    
</div>

关于javascript - 如何在多个地方使用toggle Class功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39278915/

相关文章:

jquery - 如何修复 Bootstrap 搜索/过滤栏中的动态滚动?

javascript - 长按动态内容的 jQuery 插件

html - CSS:流体边栏 - 流体内容

javascript - 如何在 Javascript 中将数组值或对象复制到另一个变量而不复制其引用?

javascript - 寻找类似于 facebook 的文本区域调整大小插件

javascript - 将 JSON 响应状态发布到服务器

css - 我的导航栏未在移动设备上显示

javascript - 在 PHP 中调用 Javascript 函数

jquery - 我的 jQuery 中的变量范围问题

javascript - 处理输入类型文件模式中的按键