热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

单击时动态创建
  • 元素-Dynamicallycreate
  • elementonclick
  • Ihavethefollowingonhtml我在html上有以下内容<html><head><scriptsrc..3003_Tes

    I have the following on html

    我在html上有以下内容

    
    
        
        
    
        
    
    
    
    

    I am trying to get write a function to handle the event when any of the list element get clicked. But it doesn't seems to be triggering at all when I click on the

  • element.

    我试图编写一个函数来处理任何列表元素被点击时的事件。但是当我点击

  • 元素时,它似乎根本没有触发。

  • $('#addressList').on('click', 'li', function() {
        alert("clicked");
        alert( $(this).text() );
    
    });
    

    The

  • elements are created dynamically through this code:

  • 元素是通过以下代码动态创建的:

  • listCOntents= $("
  • " + addresses[i].lable + "
  • "); jQuery('#addressList').append(listContents);

    And I verified through my browser's console that they are being created correctly as such

    我通过浏览器的控制台验证了它们是否正确创建

    outerHTML: "
  • 6 Cashew Crescent
    6 Cashew Crescent. (S)679751
  • "

    Let's ignore about the dynamic list first. The thing is even my static list element are not responding to the event handler upon click. Have been trying to figure out the problem for a couple of hours now..

    我们先忽略动态列表。甚至我的静态列表元素在点击时没有响应事件处理程序。一直试图找出问题几个小时了..

    I have created a jsfiddle for my static li element not working https://jsfiddle.net/tmu50t9z/

    我为我的静态li元素创建了一个jsfiddle不工作https://jsfiddle.net/tmu50t9z/

    jsfiddle to my whole code > https://jsfiddle.net/8wwnx64x/

    jsfiddle to my code> https://jsfiddle.net/8wwnx64x/

    2 个解决方案

    #1


    0  

    $('#addressList li').on('click', function() {
        alert("clicked");
        alert( $(this).text() );
    
    });
    

    #2


    0  

    Work code, try this

    工作代码,试试这个

    HTML

    Javascript

    var a=0;
    $('#aaa').click(function() {
     $('#addressList').append('
  • 6 Cashew Crescent
    6 Cashew Crescent. (S)679751
  • '); a++ }); $( "#addressList" ).on( "click", "li", function( event ) { event.preventDefault(); console.log( $( this ).text() ); });

    推荐阅读
    author-avatar
    exu8145079
    这个家伙很懒,什么也没留下!
    PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
    Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有