简体   繁体   English

如何区分在DOM函数中使用的相同div?

[英]How to differentiate identical divs for use in a DOM function?

I have a set of "cards" on my front end with a front side and a back side, these cards are created from a forEach script meaning there is a potentially different quantity each time and look something like this. 我的前端有一组“卡片”,分别带有正面和背面,这些卡片是通过forEach脚本创建的,这意味着每次都有可能不同的数量,看起来像这样。

<div class="card-container" onclick="flip()">
                    <div class="card">   
                        <div class="front">
                                <p> front of card </p>
                        </div>
                        <div class="back">
                                <p> back of card</p>
                        </div>
                    </div>
            </div>

As you may have noticed, there is a flip() function, this displays the back of the card and hides the front and vice versa when clicked a second time. 您可能已经注意到,有一个flip()函数,当第二次单击时,它会显示卡的背面并隐藏正面,反之亦然。 I would like this function only to apply to the current card, that is, the one that the user clicks, at the moment when I click it it turns over all of the cards. 我希望此功能仅适用于当前卡,即用户单击的卡,当我单击该卡时,它将翻转所有卡。

function flip() {
    $('.card').toggleClass('flipped');
}

My first solution was to try to increment the class names (card1, card2 etc.) and copy the function for each different class name, however the following example syntax gave me the error that the document object could not be found, which presumably relates to the fact I am using ejs. 我的第一个解决方案是尝试增加类名(card1,card2等)并为每个不同的类名复制函数,但是以下示例语法给我一个错误,即找不到文档对象,该错误可能与我正在使用ejs的事实。

var card = document.createElement('div');
card.classList.add('card');

I am hoping that there is a solution in jQuery that allows you to specify that the function only be carried out on 1 div, or on children of the selected div as this would be the neatest solution I think. 我希望jQuery中有一个解决方案,允许您指定该函数仅在1 div或所选div的子级上执行,因为这是我认为最简洁的解决方案。

Otherwise I think I need to work out a way to increment the div names through ejs. 否则,我认为我需要找到一种通过ejs增加div名称的方法。

you can do it with jquery click event handler. 您可以使用jquery click事件处理程序来实现。 find card from the clicked div instead of flipping all cards. 从单击的div中查找卡片,而不是翻转所有卡片。

Remove onclick function from html 从HTML移除onclick函数

<div class="card-container">
                    <div class="card">   
                        <div class="front">
                                <p> front of card </p>
                        </div>
                        <div class="back">
                                <p> back of card</p>
                        </div>
                    </div>
            </div>

Use below jquery 在jquery下面使用

$(function(){
  $('.card-container').on("click", function(){
     $(this).find('.card').toggleClass("flipped");
  });
});

If the cards exist at load time do this 如果卡在加载时存在,请执行此操作

$('.card-container').on("click",function() { $(this).find(".card").toggleClass('flipped'); }); 

or 要么

$('.card').on("click",function() { $(this).toggleClass('flipped'); });

If NOT then you need to delegate: 如果不是,那么您需要委托:

$(document).on("click",".card", function() { $(this).toggleClass('flipped'); }); 

$(document) can be changed to any static parent container $(document)可以更改为任何静态父容器

If you pass this in from your onclick function, it's relatively easy to use it in your flip function: 如果你通过this从你在onclick功能,这是比较容易的翻页功能的使用方法:

 function flip(element) { $(element).toggleClass('flipped'); } 
 .card-container { background-color: rgba(255, 245, 250, 0.9); border-top: solid 10px rgb(50, 200, 100); box-shadow: 0px 2px 1px 1px rgba(10, 10, 10, 0.3); padding: 10px; margin-bottom: 20px; position: relative; top: 0; transition: all 0.3s ease; } .flipped { box-shadow: 0px 6px 6px 3px rgba(10, 10, 10, 0.2); top: -5px; } body { background-color: #e3e3e3; font-family: arial; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="card-container" onclick="flip(this)"> <div class="card"> <div class="front"> <p> front of card </p> </div> <div class="back"> <p> back of card</p> </div> </div> </div> <div class="card-container" onclick="flip(this)"> <div class="card"> <div class="front"> <p> front of card </p> </div> <div class="back"> <p> back of card</p> </div> </div> </div> 

I added some styling just to illustrate when the cards get the flipped class. 我添加了一些样式,只是为了说明卡片何时获得flipped类。

You can only target the selected div by modifying the flip function to 您只能通过将翻转功能修改为来定位选定的div

function flip(e) {
    $(e.target).toggleClass('flipped');
}

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

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