eventlistener javascript问题

eventlistener javascript问题,javascript,html,Javascript,Html,我正在努力学习Javascript,目前正在开发AddEventListener 我想做的是添加一个新行,到目前为止,它是有效的 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> <style> .colorOrange { background-colo

我正在努力学习Javascript,目前正在开发AddEventListener

我想做的是添加一个新行,到目前为止,它是有效的

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <style>
        .colorOrange {
            background-color: orange;
        }

        .colorBlue {
            background-color: blue;
        }

        .colorYellow {
            background-color: yellow;
        }

        .colorGray {
            background-color: gray;
        }

        .colorRed {
            background-color: red;
        }

        .colorGreen {
            background-color: green;
        }

        .colorWhite {
            background-color: white;
        }

        #main {
            margin: 0 auto;
            width: 325px;
            text-align: center;
            background-color: gray;
        }

        .row {
            width: 300px;
            padding: 10px;
            border: 1px solid black;
            display: block;
        }

        .hideButton, .mainText, .deleteButton {
            margin: 0 auto;
            padding: 10px;
            border: 1px solid black;
            display: inline;
        }
        .btn {

        }
    </style>
</head>
<body>


    <div id="main">
        <div class="AddBtn btn">Add</div>
        <input type="text" id="txtBox" name="text till ruta" />

    </div>
    <script>
        var rownr = 0;

        function addListeners() {
            var addButton = document.getElementsByClassName('AddBtn');
            for (var i = 0; i < addButton.length; i++) {
                var addBtn = addButton[i];
                addBtn.addEventListener('click', function () {
                    var elBtn = event.srcElement;
                    var valueBtn = elBtn.textContent;
                    alert(valueBtn);

                    hideOrShow();
                    addRow();

                    function addRow() {
                        switch (valueBtn) {
                            case "Add":

                                var input = document.getElementById('txtBox').value;
                                rownr++;

                                var div = document.createElement('div');
                                div.className = "row";
                                document.getElementById("main").appendChild(div);

                                var div2 = document.createElement('div');
                                div2.className = "hideButton colorGreen";
                                var tx = document.createTextNode("<");
                                div2.appendChild(tx);
                                div2.addEventListener('click', hideOrShow, false);
                                div.appendChild(div2);

                                var div3 = document.createElement("div");
                                if (input.toLowerCase() == "red") {
                                    div3.className = "mainText colorRed";
                                }
                                else if (input.toLowerCase() == "orange") {
                                    div3.className = "mainText colorOrange";
                                }
                                else if (input.toLowerCase() == "blue") {
                                    div3.className = "mainText colorBlue";
                                }
                                else if (input.toLowerCase() == "yellow") {
                                    div3.className = "mainText colorYellow";
                                }
                                else if (input.toLowerCase() == "gray") {
                                    div3.className = "mainText colorGray";
                                } else {
                                    div3.className = "mainText colorWhite";
                                }
                                tx = document.createTextNode(rownr + " " + input);
                                div3.appendChild(tx);
                                div.appendChild(div3);

                                var div4 = document.createElement("div");
                                div4.className = "deleteButton colorRed";
                                tx = document.createTextNode("X");
                                div4.appendChild(tx);
                                //div4.addEventListener('click', deleBtn, false);
                                div.appendChild(div4);

                                var linebreak = document.createElement("br");
                                div.appendChild(linebreak);
                            default:
                        }
                    }

.橙色{
背景颜色:橙色;
}
.colorBlue{
背景颜色:蓝色;
}
.黄色{
背景颜色:黄色;
}
.colorGray{
背景颜色:灰色;
}
.红色{
背景色:红色;
}
.colorGreen{
背景颜色:绿色;
}
.colorWhite{
背景色:白色;
}
#主要{
保证金:0自动;
宽度:325px;
文本对齐:居中;
背景颜色:灰色;
}
.行{
宽度:300px;
填充:10px;
边框:1px纯黑;
显示:块;
}
.hideButton、.mainText、.deleteButton{
保证金:0自动;
填充:10px;
边框:1px纯黑;
显示:内联;
}
.btn{
}
添加
var-rownr=0;
函数addListeners(){
var addButton=document.getElementsByClassName('AddBtn');
对于(变量i=0;ivar tx=document.createTextNode(“这是我对javascript的修改。我在你的评论中解释了我的解决方案,但如果有说明的话可能会更清楚一些

addListeners
函数中,我删除了
hideOrShow
调用,因为它不应该在add按钮中调用

接下来,我删除了
hideOrShow
方法中的for循环,因为您实际上只是在调用方之后。我还删除了
addEventListener
调用,因为您在该元素上已经有一个事件侦听器,所以无需再添加一个

    var rownr = 0;
    function addListeners() {
        var addButton = document.getElementsByClassName('AddBtn');
        for (var i = 0; i < addButton.length; i++) {
            var addBtn = addButton[i];
            addBtn.addEventListener('click', function () {
                var elBtn = event.srcElement;
                var valueBtn = elBtn.textContent;
                alert(valueBtn);

                //hideOrShow();
                addRow();

                function addRow() {
                    switch (valueBtn) {
                        case "Add":

                            var input = document.getElementById('txtBox').value;
                            rownr++;

                            var div = document.createElement('div');
                            div.className = "row";
                            document.getElementById("main").appendChild(div);

                            var div2 = document.createElement('div');
                            div2.className = "hideButton colorGreen";
                            var tx = document.createTextNode("<");
                            div2.appendChild(tx);
                            div2.addEventListener('click', hideOrShow, false);
                            div.appendChild(div2);
                            var div3 = document.createElement("div");
                            if (input.toLowerCase() == "red") {
                                div3.className = "mainText colorRed";
                            }
                            else if (input.toLowerCase() == "orange") {
                                div3.className = "mainText colorOrange";
                            }
                            else if (input.toLowerCase() == "blue") {
                                div3.className = "mainText colorBlue";
                            }
                            else if (input.toLowerCase() == "yellow") {
                                div3.className = "mainText colorYellow";
                            }
                            else if (input.toLowerCase() == "gray") {
                                div3.className = "mainText colorGray";
                            } else {
                                div3.className = "mainText colorWhite";
                            }
                            tx = document.createTextNode(rownr + " " + input);
                            div3.appendChild(tx);
                            div.appendChild(div3);

                            var div4 = document.createElement("div");
                            div4.className = "deleteButton colorRed";
                            tx = document.createTextNode("X");
                            div4.appendChild(tx);
                            //div4.addEventListener('click', deleBtn, false);
                            div.appendChild(div4);

                            var linebreak = document.createElement("br");
                            div.appendChild(linebreak);
                        default:
                    }
                }
               function hideOrShow() {
                    var hideButton = document.getElementsByClassName('hideButton');
                    var hideElBtn = event.srcElement;
                    var valueHideBtn = hideElBtn.textContent;
                    alert(valueHideBtn);
                }
            }, false);
        }
    } 
    window.onload = addListeners;
var rownr=0;
函数addListeners(){
var addButton=document.getElementsByClassName('AddBtn');
对于(变量i=0;ivar tx=document.createTextNode(“你的错误在于你的
hideOrShow
函数。你的hideButton上已经有了一个事件监听器,但是你循环并在该函数中添加了第二个事件监听器,你添加的第二个偶数监听器选择文本内容。你可以去掉该事件监听器,你会没事的。好的。这就是为什么它会这样做,im reall非常感谢你花时间向我展示了我做错了什么。所以我可以继续学习更多关于javascript的知识。
    var rownr = 0;
    function addListeners() {
        var addButton = document.getElementsByClassName('AddBtn');
        for (var i = 0; i < addButton.length; i++) {
            var addBtn = addButton[i];
            addBtn.addEventListener('click', function () {
                var elBtn = event.srcElement;
                var valueBtn = elBtn.textContent;
                alert(valueBtn);

                //hideOrShow();
                addRow();

                function addRow() {
                    switch (valueBtn) {
                        case "Add":

                            var input = document.getElementById('txtBox').value;
                            rownr++;

                            var div = document.createElement('div');
                            div.className = "row";
                            document.getElementById("main").appendChild(div);

                            var div2 = document.createElement('div');
                            div2.className = "hideButton colorGreen";
                            var tx = document.createTextNode("<");
                            div2.appendChild(tx);
                            div2.addEventListener('click', hideOrShow, false);
                            div.appendChild(div2);
                            var div3 = document.createElement("div");
                            if (input.toLowerCase() == "red") {
                                div3.className = "mainText colorRed";
                            }
                            else if (input.toLowerCase() == "orange") {
                                div3.className = "mainText colorOrange";
                            }
                            else if (input.toLowerCase() == "blue") {
                                div3.className = "mainText colorBlue";
                            }
                            else if (input.toLowerCase() == "yellow") {
                                div3.className = "mainText colorYellow";
                            }
                            else if (input.toLowerCase() == "gray") {
                                div3.className = "mainText colorGray";
                            } else {
                                div3.className = "mainText colorWhite";
                            }
                            tx = document.createTextNode(rownr + " " + input);
                            div3.appendChild(tx);
                            div.appendChild(div3);

                            var div4 = document.createElement("div");
                            div4.className = "deleteButton colorRed";
                            tx = document.createTextNode("X");
                            div4.appendChild(tx);
                            //div4.addEventListener('click', deleBtn, false);
                            div.appendChild(div4);

                            var linebreak = document.createElement("br");
                            div.appendChild(linebreak);
                        default:
                    }
                }
               function hideOrShow() {
                    var hideButton = document.getElementsByClassName('hideButton');
                    var hideElBtn = event.srcElement;
                    var valueHideBtn = hideElBtn.textContent;
                    alert(valueHideBtn);
                }
            }, false);
        }
    } 
    window.onload = addListeners;