javascript - 我的 onclick 附加到在同一函数中被删除的类。为什么它仍然被触发?

标签 javascript html css dom

我有一个由类“tile1”触发的 onclick 事件。在该函数中,我使用了“classList.remove("tile1")”,那么如果我单击具有类“tile1”的元素,为什么它会被第二次触发?

这里有很多 css 信息,但我认为这些信息与导致问题的原因无关,但我将其包含在内,以便您可以轻松地自己复制和粘贴代码。

非常感谢任何帮助。

var galleryContainer = document.getElementsByClassName("galleryContainer");
var category = document.getElementsByClassName("category");
var subCat = document.getElementsByClassName("subCat");
var tile = document.getElementsByClassName("tile");
var tile1 = document.getElementsByClassName("tile1");
var tileImg = document.getElementsByClassName("tileImg");
var galleryWindow = document.getElementsByClassName("galleryWindow");
var albumEntry = document.getElementsByClassName("albumEntry");
var albumEntryImg = document.getElementsByClassName("albumEntryImg");
// POSSIBLE ISSUE (next two lines)
for(var index = 0; index < tile1.length; index++) {
    tile1[index].onclick = function() {
     
        //SUBCAT1a
        setTimeout(function(){
            for (var a = 0; a < subCat.length; a++) {
                subCat[a].classList.remove("subCat1");
                subCat[a].classList.remove("subCat2");
                subCat[a].classList.remove("subCat3");
                subCat[a].classList.add("subCat1a");
                subCat[a].style.transition = "linear 200ms";
            }
        }, 0);

        //SUBCAT
        setTimeout(function(){
            for (var b = 0; b < subCat.length; b++) {
                subCat[b].classList.remove("subCat1");
                subCat[b].classList.remove("subCat1a");
                subCat[b].classList.remove("subCat3");
                subCat[b].classList.add("subCat2");
                subCat[b].style.transition = "linear 1000ms";
            }
        }, 1500);

        //GALLERY CONTAINER
        setTimeout(function(){
            for (var c = 0; c < galleryContainer.length; c++) {
                galleryContainer[c].classList.remove("galleryContainer1");
                galleryContainer[c].classList.remove("galleryContainer3");
                galleryContainer[c].classList.add("galleryContainer2");
                galleryContainer[c].style.transition = "linear 500ms";
            }
        }, 1000);

        //CATEGORY
        setTimeout(function(){
            for (var d = 0; d < category.length; d++) {
                category[d].classList.remove("category1");
                category[d].classList.remove("category2");
                category[d].classList.remove("category3");
                category[d].classList.add("category1a");
                category[d].style.transition = "linear 500ms";
            }
        }, 500);

        //CATEGORY
        setTimeout(function(){
            for (var e = 0; e < category.length; e++) {
                category[e].classList.remove("category1");
                category[e].classList.remove("category1a");
                category[e].classList.remove("category3");
                category[e].classList.add("category2");
                //category[e].style.transition = "linear 0s";
            }
        }, 500);

        // POSSIBLE ISSUE (next nine lines)
        //TILE
        setTimeout(function(){
            for (var f = 0; f < tile.length; f++) {
                tile[f].classList.remove("tile1");
                tile[f].classList.remove("tile3");
                tile[f].classList.add("tile2");
                //tile[e].style.transition = "linear 0s";
            }
        }, 500);


        //TILEIMG
        setTimeout(function(){
            for (var g = 0; g < tileImg.length; g++) {
                tileImg[g].classList.remove("tileImg1");
                tileImg[g].classList.remove("tileImg3");
                tileImg[g].classList.add("tileImg2");
                tileImg[g].style.transition = "linear 500ms";
            }
        }, 500);


        //GALLERY WINDOW
        setTimeout(function(){
            for (var h = 0; h < galleryWindow.length; h++) {
                galleryWindow[h].classList.remove("galleryWindow1");
                galleryWindow[h].classList.remove("galleryWindow3");
                galleryWindow[h].classList.add("galleryWindow2");
                //galleryWindow[h].style.transition = "opacity linear 5000ms";
            }
        }, 1800);

        //ALBUM ENTRY CONTAINER
        setTimeout(function(){
             for(var i = 0; albumEntry.length; i++) {
                 albumEntry[i].classList.remove("albumEntry1");
                 albumEntry[i].classList.remove("albumEntry3");
                 albumEntry[i].classList.add("albumEntry2");
             }
        }, 1800);

        //ALBUM ENTRY IMG
        setTimeout(function(){
             for(var j = 0; albumEntryImg.length; j++) {
                 albumEntryImg[j].classList.remove("albumEntryImg1");
                 albumEntryImg[j].classList.remove("albumEntryImg3");
                 albumEntryImg[j].classList.add("albumEntryImg2");
             }
        }, 1800);
    }   
}
* {
    box-sizing: border-box;
}

body {
    margin: 0px;
    padding: 0px;
}

/* ////////// STEP 1 ////////// */

.galleryContainer1 {
    display: flex;
    width: 100vw;
    
    flex-direction: column;
    justify-content: center;
    padding-top: 1vw;
}

.category1 {
    border: 0.4vw solid #00f;
    position: relative;
    margin: 1vw;
}

.category1 h3 {
    position: absolute;
    top: -2.6vw;
    left: 1vw;
    
    font-size: 1.5vw;
    background-color: #fff;
    
    padding: 0px 1vw;
}

.subCat1 {
    display: flex;
    margin: 0.5vw;
    
    justify-content: space-around;
    align-items: center;
    flex-wrap: wrap;
}

.tile1 {
    border: 1px solid #000;
    position: relative;
    display: flex;
    margin: 1vw;
    cursor: pointer;
}

.tile1:hover {
    opacity: 0.5;
}

.tile1 h4 {
    position: absolute;
    top: 0.1vw;
    
    user-select: none;
    cursor: inherit;
    font-size: 2vw;
    font-weight: bolder;
    color: #fff;
    text-shadow: 
        0px 0px 5px #000;
}

.tileImg1 {
    position: relative;
    top: 0px;
    border: 1px solid #000;
    display: flex;
    flex: 1;
    width: 6vw;
}

#natureText {left: 3vw;}

.galleryWindow1 {
    display: flex;
    width: 0px;
    opacity: 0;
}

.galleryWindow1 h1 {
    display: none;
    color: #f0f;
}

.albumEntry1 {
    display: none;
}

.albumEntryImg1 {
    display: none;
}

/* ///// STEP 1a (transitional step) ///// */

.category1a:first-of-type {
    border: 0.4vw solid #00f;
    position: relative;
    margin: 1vw;
    
    height: 15vw;
}

.category1a {
    border: 0.4vw solid #00f;
    position: relative;
    margin: 1vw;
    
    height: 28vw;
}

.subCat1a {
    display: flex;
    margin: 0.5vw;
    
    justify-content: space-around;
    align-items: center;
    flex-wrap: wrap;
    
    opacity: 0;
}

/* ////////// STEP 2 ////////// */

.galleryContainer2 {
    display: flex;
    width: 20vw;
    
    flex-direction: column;
    justify-content: center;
    padding-top: 1vw;
}

.category2:first-of-type {
    /*min-height: 190px;*/
    height: 14vw;
}

.category2 {
    border: 0.4vw solid #00f;
    position: relative;
    margin: 1vw;
    
    /*min-height: 360px;*/
    height: 27vw;
}

.category2 h3 {
    position: absolute;
    top: -2.6vw;
    left: 1vw;
    
    font-size: 1.5vw;
    background-color: #fff;
    
    padding: 0px 1vw;
}

.subCat2 {
    display: flex;
    margin: 0.5vw;
    
    justify-content: flex-start;
    flex-direction: column;
}

.tile2 {
    position: relative;
    display: flex;
    margin: 0vw;
    user-select: none;
}

.tile2 h4 {
    display: flex;
    margin: 0.2vw;
    
    font-size: 1.3vw;
    color: #888;
    cursor: pointer;
}

.tile2 h4:hover {
    color: #000;
}

.tileImg2 {
    position: relative;
    top: 0.8vw;
    /*top: 11px;*/
    border: 1px solid transparent;
    display: flex;
    flex: 1;
    width: 0vw;
}
<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Test 5 - transitions</title>
    <link href="css/T5.css" rel="stylesheet" type="text/css">
</head>
<body>
    <section class="galleryContainer galleryContainer1">
        <div class="category category1">
            <h3>TYPE:</h3>
            <div class="subCat subCat1">
                <div class="tile tile1">
                    <div>
                        <img class="tileImg tileImg1" src="../../Image Bank/Location/London/1 london-people-sky-34208.jpg">
                        <img class="tileImg tileImg1" src="../../Image Bank/Location/London/1 london-people-sky-34208.jpg">
                    </div>
                    <div>
                        <img class="tileImg tileImg1" src="../../Image Bank/Location/London/1 london-people-sky-34208.jpg">
                        <img class="tileImg tileImg1" src="../../Image Bank/Location/London/1 london-people-sky-34208.jpg">
                    </div>
                    <h4 class="tileText tileText1" id="natureText">Nature</h4>
                </div>
            </div>
        </div>
    </section>
    <section class="galleryWindow galleryWindow1">
        <h1>TEST:</h1>
    </section>
    <script src="js/T5.js"></script>
</body>
</html>

最佳答案

注册事件监听器时,您正在注册一个元素。只要该元素有一个附加的点击事件监听器,每次单击该元素时都会触发事件监听器的回调。该元素可以添加和/或删除类,事件监听器不会受到影响。请记住,您只是使用该类来找到您想要将事件监听器附加到的元素。找到它后,删除您用来找到它的类是无关紧要的。

要删除元素监听器,请使用 removeEventListener 并将相同的引用传递给用于注册事件监听器的回调。

因为您已经使用了onclick,所以您可以将函数引用设置为null

... tile[f].onclick = null; ...

var galleryContainer = document.getElementsByClassName("galleryContainer");
var category = document.getElementsByClassName("category");
var subCat = document.getElementsByClassName("subCat");
var tile = document.getElementsByClassName("tile");
var tile1 = document.getElementsByClassName("tile1");
var tileImg = document.getElementsByClassName("tileImg");
var galleryWindow = document.getElementsByClassName("galleryWindow");
var albumEntry = document.getElementsByClassName("albumEntry");
var albumEntryImg = document.getElementsByClassName("albumEntryImg");
// POSSIBLE ISSUE (next two lines)
for(var index = 0; index < tile1.length; index++) {
    tile1[index].onclick = function() {
     
        //SUBCAT1a
        setTimeout(function(){
            for (var a = 0; a < subCat.length; a++) {
                subCat[a].classList.remove("subCat1");
                subCat[a].classList.remove("subCat2");
                subCat[a].classList.remove("subCat3");
                subCat[a].classList.add("subCat1a");
                subCat[a].style.transition = "linear 200ms";
            }
        }, 0);

        //SUBCAT
        setTimeout(function(){
            for (var b = 0; b < subCat.length; b++) {
                subCat[b].classList.remove("subCat1");
                subCat[b].classList.remove("subCat1a");
                subCat[b].classList.remove("subCat3");
                subCat[b].classList.add("subCat2");
                subCat[b].style.transition = "linear 1000ms";
            }
        }, 1500);

        //GALLERY CONTAINER
        setTimeout(function(){
            for (var c = 0; c < galleryContainer.length; c++) {
                galleryContainer[c].classList.remove("galleryContainer1");
                galleryContainer[c].classList.remove("galleryContainer3");
                galleryContainer[c].classList.add("galleryContainer2");
                galleryContainer[c].style.transition = "linear 500ms";
            }
        }, 1000);

        //CATEGORY
        setTimeout(function(){
            for (var d = 0; d < category.length; d++) {
                category[d].classList.remove("category1");
                category[d].classList.remove("category2");
                category[d].classList.remove("category3");
                category[d].classList.add("category1a");
                category[d].style.transition = "linear 500ms";
            }
        }, 500);

        //CATEGORY
        setTimeout(function(){
            for (var e = 0; e < category.length; e++) {
                category[e].classList.remove("category1");
                category[e].classList.remove("category1a");
                category[e].classList.remove("category3");
                category[e].classList.add("category2");
                //category[e].style.transition = "linear 0s";
            }
        }, 500);

        // POSSIBLE ISSUE (next nine lines)
        //TILE
        setTimeout(function(){
            for (var f = 0; f < tile.length; f++) {
                tile[f].classList.remove("tile1");
                tile[f].onclick = null;
                tile[f].classList.remove("tile3");
                tile[f].classList.add("tile2");
                //tile[e].style.transition = "linear 0s";
            }
        }, 500);


        //TILEIMG
        setTimeout(function(){
            for (var g = 0; g < tileImg.length; g++) {
                tileImg[g].classList.remove("tileImg1");
                tileImg[g].classList.remove("tileImg3");
                tileImg[g].classList.add("tileImg2");
                tileImg[g].style.transition = "linear 500ms";
            }
        }, 500);


        //GALLERY WINDOW
        setTimeout(function(){
            for (var h = 0; h < galleryWindow.length; h++) {
                galleryWindow[h].classList.remove("galleryWindow1");
                galleryWindow[h].classList.remove("galleryWindow3");
                galleryWindow[h].classList.add("galleryWindow2");
                //galleryWindow[h].style.transition = "opacity linear 5000ms";
            }
        }, 1800);

        //ALBUM ENTRY CONTAINER
        setTimeout(function(){
             for(var i = 0; albumEntry.length; i++) {
                 albumEntry[i].classList.remove("albumEntry1");
                 albumEntry[i].classList.remove("albumEntry3");
                 albumEntry[i].classList.add("albumEntry2");
             }
        }, 1800);

        //ALBUM ENTRY IMG
        setTimeout(function(){
             for(var j = 0; albumEntryImg.length; j++) {
                 albumEntryImg[j].classList.remove("albumEntryImg1");
                 albumEntryImg[j].classList.remove("albumEntryImg3");
                 albumEntryImg[j].classList.add("albumEntryImg2");
             }
        }, 1800);
    }   
}
* {
    box-sizing: border-box;
}

body {
    margin: 0px;
    padding: 0px;
}

/* ////////// STEP 1 ////////// */

.galleryContainer1 {
    display: flex;
    width: 100vw;
    
    flex-direction: column;
    justify-content: center;
    padding-top: 1vw;
}

.category1 {
    border: 0.4vw solid #00f;
    position: relative;
    margin: 1vw;
}

.category1 h3 {
    position: absolute;
    top: -2.6vw;
    left: 1vw;
    
    font-size: 1.5vw;
    background-color: #fff;
    
    padding: 0px 1vw;
}

.subCat1 {
    display: flex;
    margin: 0.5vw;
    
    justify-content: space-around;
    align-items: center;
    flex-wrap: wrap;
}

.tile1 {
    border: 1px solid #000;
    position: relative;
    display: flex;
    margin: 1vw;
    cursor: pointer;
}

.tile1:hover {
    opacity: 0.5;
}

.tile1 h4 {
    position: absolute;
    top: 0.1vw;
    
    user-select: none;
    cursor: inherit;
    font-size: 2vw;
    font-weight: bolder;
    color: #fff;
    text-shadow: 
        0px 0px 5px #000;
}

.tileImg1 {
    position: relative;
    top: 0px;
    border: 1px solid #000;
    display: flex;
    flex: 1;
    width: 6vw;
}

#natureText {left: 3vw;}

.galleryWindow1 {
    display: flex;
    width: 0px;
    opacity: 0;
}

.galleryWindow1 h1 {
    display: none;
    color: #f0f;
}

.albumEntry1 {
    display: none;
}

.albumEntryImg1 {
    display: none;
}

/* ///// STEP 1a (transitional step) ///// */

.category1a:first-of-type {
    border: 0.4vw solid #00f;
    position: relative;
    margin: 1vw;
    
    height: 15vw;
}

.category1a {
    border: 0.4vw solid #00f;
    position: relative;
    margin: 1vw;
    
    height: 28vw;
}

.subCat1a {
    display: flex;
    margin: 0.5vw;
    
    justify-content: space-around;
    align-items: center;
    flex-wrap: wrap;
    
    opacity: 0;
}

/* ////////// STEP 2 ////////// */

.galleryContainer2 {
    display: flex;
    width: 20vw;
    
    flex-direction: column;
    justify-content: center;
    padding-top: 1vw;
}

.category2:first-of-type {
    /*min-height: 190px;*/
    height: 14vw;
}

.category2 {
    border: 0.4vw solid #00f;
    position: relative;
    margin: 1vw;
    
    /*min-height: 360px;*/
    height: 27vw;
}

.category2 h3 {
    position: absolute;
    top: -2.6vw;
    left: 1vw;
    
    font-size: 1.5vw;
    background-color: #fff;
    
    padding: 0px 1vw;
}

.subCat2 {
    display: flex;
    margin: 0.5vw;
    
    justify-content: flex-start;
    flex-direction: column;
}

.tile2 {
    position: relative;
    display: flex;
    margin: 0vw;
    user-select: none;
}

.tile2 h4 {
    display: flex;
    margin: 0.2vw;
    
    font-size: 1.3vw;
    color: #888;
    cursor: pointer;
}

.tile2 h4:hover {
    color: #000;
}

.tileImg2 {
    position: relative;
    top: 0.8vw;
    /*top: 11px;*/
    border: 1px solid transparent;
    display: flex;
    flex: 1;
    width: 0vw;
}
<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Test 5 - transitions</title>
    <link href="css/T5.css" rel="stylesheet" type="text/css">
</head>
<body>
    <section class="galleryContainer galleryContainer1">
        <div class="category category1">
            <h3>TYPE:</h3>
            <div class="subCat subCat1">
                <div class="tile tile1">
                    <div>
                        <img class="tileImg tileImg1" src="../../Image Bank/Location/London/1 london-people-sky-34208.jpg">
                        <img class="tileImg tileImg1" src="../../Image Bank/Location/London/1 london-people-sky-34208.jpg">
                    </div>
                    <div>
                        <img class="tileImg tileImg1" src="../../Image Bank/Location/London/1 london-people-sky-34208.jpg">
                        <img class="tileImg tileImg1" src="../../Image Bank/Location/London/1 london-people-sky-34208.jpg">
                    </div>
                    <h4 class="tileText tileText1" id="natureText">Nature</h4>
                </div>
            </div>
        </div>
    </section>
    <section class="galleryWindow galleryWindow1">
        <h1>TEST:</h1>
    </section>
    <script src="js/T5.js"></script>
</body>
</html>

关于javascript - 我的 onclick 附加到在同一函数中被删除的类。为什么它仍然被触发?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54131121/

相关文章:

html - 类级别的动态CSS

html - 将导航栏保持在图像 slider 上方 wordpress 主题

javascript - 使用脚本在 IE11 中添加备用样式表

javascript - JS(template Literal-)生成html,更新DOM?

javascript - JQuery 手机。网站上只有一个 https 页面

html - 防止div溢出

html - 淡入颜色按钮从 btn-primary 到 btn-success

javascript - 实现标签!如何重命名使用 Jquery 动态生成的选项卡?

javascript - Node.js 与 Python 中的剩余部分

html - 相对于表格单元格的定位