【问题标题】:How to use the same function in javascript, in the same classes in html [duplicate]如何在javascript中使用相同的函数,在html中的相同类中[重复]
【发布时间】:2019-04-01 12:38:14
【问题描述】:

除了我是 JavaScript 新手之外,我有几天没有找到答案的问题。

$(document).on('click', '.content-click', function(){
    $(".content-click span").toggleClass("clicked"),
    $(".content-view").toggleClass("viewed");
    $(this).show();
});
.content-click {
    display: block;
    width: 100%;
    height: 2rem;
    padding: 0.375rem 0.75rem;
    font-size: .75rem;
    font-weight: 500;
    line-height: 1.5;
    color: #495057;
    background-color: #fff;
    background-clip: padding-box;
    border: 1px solid #ced4da;
    border-radius: 0.25rem;
    cursor: pointer;
}

.content-click p {
    display: inline;
    margin: 0;
}

.content-click span {
    width: 10px;
    height: 10px;
    position: relative;
    top: 5px;
    float: right;
    vertical-align: middle;
    background: url(../img/arrow.png) no-repeat;
    transition: all 0.3s ease;
    transform: rotate(0deg);
}

.content-click span.clicked {
    transform: rotate(90deg);
} /*button click styling */

.content-view {
    display: block;
    width: 100%;
    height: 0;
    border: 0px solid #ebebeb;
    box-sizing: border-box;
    margin-top: 0rem;
    margin-bottom: 0rem;
    position: relative;
    border-radius: .25rem;
    padding: 0;
    font-size: 0;
    font-weight: 500;
    opacity: 0;
    transition: all 0.2s ease;
}

.content-view::after {
    content: '';
    width: 10px;
    height: 10px;
    border-top: 1px solid #ebebeb;
    border-right: 0px solid #ebebeb;
    border-bottom: 0px solid #ebebeb;
    border-left: 1px solid #ebebeb;
    position: absolute;
    left: 95%;
    top: 0%;
    margin-top: -6px;
    margin-left: -6px;
    transform: rotate(45deg);
    background-color: #fff;
}

.content-view.viewed {
    height: auto;
    border: 1px solid #ebebeb;
    margin-top: .25rem;
    margin-bottom: 1rem;
    font-size: .75rem;
    padding: 1rem;
    opacity: 1;
} /*description area-text styling*/
<div class="container">
            <div class="content-click" style="margin:.25rem;">
                <div id="content-1">
                    <p>First Item list...</p>
                    <span></span>
                </div>
            </div>
            <div class="content-view">
                <div id="view-1">
                    <p>Description...</p>
                </div>
            </div>
            <div class="content-click" style="margin:.25rem;">
                <div id="content-2">
                    <p>First Item list...</p>
                    <span></span>
                </div>
            </div>
            <div class="content-view">
                <div id="view-2">
                    <p>Description...</p>
                </div>
            </div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">
</script>
在这种情况下,我创建了按钮,如果任何用户单击它,就会弹出文本区域或描述窗口。但是在这种情况下,我只有 2 个项目,这很容易维护,但是后来,如果我有 100 个项目怎么办?这意味着我需要为每个项目提供 100 个 ID,对吗?

一旦我将类名添加为 .toggleClass 目标,具有完全相同类的相同项目将弹出描述,即使我只单击第一个按钮。抱歉问了个愚蠢的问题。

【问题讨论】:

    标签: javascript jquery html css


    【解决方案1】:

    你需要维护你的点击事件的范围,这样每当你点击一个项目时,只有下一个描述展开,而不是全部展开。

    代码如下:

    $(document).on('click', '.content-click', function(){
        $(this).find("span").toggleClass("clicked"); // this finds the child element 'span'
        $(this).next().toggleClass("viewed"); // .next() is the selector for the next sibling element;
        $(this).show();
    });
    .content-click {
        display: block;
        width: 100%;
        height: 2rem;
        padding: 0.375rem 0.75rem;
        font-size: .75rem;
        font-weight: 500;
        line-height: 1.5;
        color: #495057;
        background-color: #fff;
        background-clip: padding-box;
        border: 1px solid #ced4da;
        border-radius: 0.25rem;
        cursor: pointer;
    }
    
    .content-click p {
        display: inline;
        margin: 0;
    }
    
    .content-click span {
        width: 10px;
        height: 10px;
        position: relative;
        top: 5px;
        float: right;
        vertical-align: middle;
        background: url(../img/arrow.png) no-repeat;
        transition: all 0.3s ease;
        transform: rotate(0deg);
    }
    
    .content-click span.clicked {
        transform: rotate(90deg);
    } /*button click styling */
    
    .content-view {
        display: block;
        width: 100%;
        height: 0;
        border: 0px solid #ebebeb;
        box-sizing: border-box;
        margin-top: 0rem;
        margin-bottom: 0rem;
        position: relative;
        border-radius: .25rem;
        padding: 0;
        font-size: 0;
        font-weight: 500;
        opacity: 0;
        transition: all 0.2s ease;
    }
    
    .content-view::after {
        content: '';
        width: 10px;
        height: 10px;
        border-top: 1px solid #ebebeb;
        border-right: 0px solid #ebebeb;
        border-bottom: 0px solid #ebebeb;
        border-left: 1px solid #ebebeb;
        position: absolute;
        left: 95%;
        top: 0%;
        margin-top: -6px;
        margin-left: -6px;
        transform: rotate(45deg);
        background-color: #fff;
    }
    
    .content-view.viewed {
        height: auto;
        border: 1px solid #ebebeb;
        margin-top: .25rem;
        margin-bottom: 1rem;
        font-size: .75rem;
        padding: 1rem;
        opacity: 1;
    } /*description area-text styling*/
    <div class="container">
                <div class="content-click" style="margin:.25rem;">
                    <div id="content-1">
                        <p>First Item list...</p>
                        <span></span>
                    </div>
                </div>
                <div class="content-view">
                    <div id="view-1">
                        <p>Description...</p>
                    </div>
                </div>
                <div class="content-click" style="margin:.25rem;">
                    <div id="content-2">
                        <p>First Item list...</p>
                        <span></span>
                    </div>
                </div>
                <div class="content-view">
                    <div id="view-2">
                        <p>Description...</p>
                    </div>
                </div>
    </div>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">
    </script>

    【讨论】:

    • 非常感谢先生,它解决了我的问题。
    【解决方案2】:

    当你有这样的关系时,不要使用ids 来查找元素。

    首先,捕获事件对象。

    $(document).on('click', '.content-click', function(event){
    

    然后用它来获取被点击的元素

    var $clicked = $(event.currentTarget);
    

    …然后使用parentfind 等函数导航到您要处理的关联元素。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-10
      • 2010-12-29
      • 1970-01-01
      • 2022-01-22
      • 1970-01-01
      • 1970-01-01
      • 2017-12-27
      • 1970-01-01
      相关资源
      最近更新 更多