Click the JavaScript event delegate to execute 1 for the first time, and click to execute 1 and 2 for the second time.

layout is ul > li > I and then dynamically added li uses event delegate method
now the problem is that every time the page is refreshed, the first click is executed to console.log ("1")
the dynamically generated elements of console.log ("1") console.log ("22222222222222")
are executed only on the second click. It is also the first time to click console.log ("1")
and the second time to click console.log ("1") console.log ("22222222222222)

how, it has always been console.log ("1") console.log ("22222222222222")

            let aul = document.querySelector("-sharpcommentList")
            


            aul.addEventListener("click",function(ev){
                var target = ev.target;
                while(target !== aul ){
                    if(target.tagName.toLowerCase() == "li"){
                        aa()
                        console.log("1")
                        break;
                    }
                    target = target.parentNode;
                }
            })
 
            function aa(){
 
                let zanli = document.getElementsByTagName("li")

                for (var i = zanli.length - 1; i >= 0; i--) {
                    let num = i
                    zanli[i].addEventListener("click",function(ev){
                        console.log("222222222222")
                        zaning(num)
                    })
 
                }
            }
 
            function zaning(num){
                let zanli = document.getElementsByTagName("li")
                for (let i = zanli.length - 1; i >= 0; i--) {
 
                    let findi = zanli[i].querySelector("i")
                    
                    if (i==num) {
                        if(findi.className == "pt_praise active") {
                            return false;
                        }
                        findi.classList.add("active")
                        var numm = findi.innerHTML
                        findi.innerHTML = Number(numm)+1;
                    }
                }
            }    
Mar.28,2021

take a look at the event delegate


    <div class="page-iscroll" id="page-iscroll">
        <div class="wrapper">
            <div class="chat_mod">
                <!-- <div class="not_mod">...</div> -->
                <ul id="commentList" class="we_chat comment_list">
                    <li>
                        <div class="img"><img src="images/video_img/1.jpg" alt=""></div>
                        <div class="con">
                            <span></span>
                            <span><strong>05-23 14:22</strong><i class="pt_praise">6</i></span>
                            <span></span>
                        </div>
                    </li>
                    <li>
                        <div class="img"><img src="images/video_img/1.jpg" alt=""></div>
                        <div class="con">
                            <span></span>
                            <span><strong>05-23 14:22</strong><i class="pt_praise">0</i></span>
                            <span></span>
                        </div>
                    </li>
                </ul>
            </div>
        </div>
    </div>
 
</section>
 
<footer class="appointment_footer">
    <div class="item"><input class="form_control mywords" id="mywords" type="text" maxlength="140"  value="" placeholder="..."></div>
    <div class="item"><a href="javascript:void(0);" class="btn sendBt" id="sendBtn"onclick="sendMessage()"></a></div>
    <div class="item"><i class="btn_favs_icon btnfavs"></i></div>
    <div class="item"><i class="btn_share_icon btnshare"></i></div>
</footer>
<script>
  

        //   
        // myScroll.scrollTo(0, 0); 
        // myScroll.refresh(); 
        // myScrollChat.scrollToElement(  
        //     
        //     document.querySelector('-sharpchatArea li:last-child'), 500, null, true
        // ); 


        let myScrollChat;
        let commentList=document.getElementById("commentList");
        let ptPraise = document.querySelector(".pt_praise");
        let aul = document.querySelector("-sharpcommentList");
        let zanli = document.getElementsByTagName("li");

        function loaded() {
            myScrollChat = new IScroll('-sharppage-iscroll', {mouseWheel: true, tap: true,click:true});  
        }

        chatWindowHeight();
        loaded();

        window.addEventListener('resize', function () {
            chatWindowHeight();
            loaded();
        }, false);

 
        aa();

        function aa(){
            for (var i = zanli.length - 1; i >= 0; i--) {
                let num = i
                zanli[i].addEventListener("click",function(ev){
                    var target = ev.target;
                    if(target.tagName.toLowerCase() == 'i'){
                        zaning(num)
                    }
                })
            }
        }

        function zaning(num){
            for (let i = zanli.length - 1; i >= 0; i--) {
                let findi = zanli[i].querySelector("i")
                if (i==num) {
                    if(findi.className == 'pt_praise active') {
                        return false;
                    }
                    findi.classList.add("active")
                    var numm = findi.innerHTML
                    findi.innerHTML = Number(numm)+1;
                }
            }
        }    
        
        function addMessage(message){
            var strVar = "";
                strVar += "<li>";
                strVar += "<div class=\"img\">";
                strVar += "    <img src=\"images/video_img/1.jpg\" alt=\"\">";
                strVar += "<\/div>";
                strVar += "<div class=\"con\">";
                strVar += "    <span><\/span>";
                strVar += "    <span><strong>05-23 14:22<\/strong><i class=\"pt_praise\">0<\/i><\/span>";
                strVar += "    <span>";
                strVar += message;
                strVar += "    <\/span>";
                strVar += "<\/div>";
                strVar += "<\/li>";

            if (message === '') {
                layer.msg('...');
            } else {
                commentList.innerHTML += strVar;
                aa();
                
                //
                setTimeout(function () {
                    myScrollChat.refresh();  
                    myScrollChat.scrollToElement(
                        document.querySelector('-sharpcommentList li:last-child'), 500, null, true
                    ); 
                }, 0);

            }
            document.getElementById("mywords").value = '';
        }

        function sendMessage(){
            var msg=document.getElementById("mywords").value;
            addMessage(msg); 
        }

 

</script>
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b3e8fe-2c418.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b3e8fe-2c418.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?