如何在javascript的相同类中使用javascript中的相同函数

时间:2018-10-28 11:23:42

标签: javascript jquery html css

除了我不熟悉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目标后,具有完全相同类的相同项将弹出描述,即使我仅单击第一个按钮也是如此。很抱歉提出愚蠢的问题。

2 个答案:

答案 0 :(得分:0)

您需要维护click事件的范围,以便每当您单击一个项目时,只会展开下一个说明,而不会全部展开。

代码如下:

$(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>

答案 1 :(得分:0)

当您有这样的关系时,请勿使用id来查找元素。

首先,捕获事件对象。

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

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

var $clicked = $(event.currentTarget);

…,然后使用parentfind之类的功能导航到要处理的关联元素。