简体   繁体   English

在动态添加的 DOM 元素上使用点击事件

[英]Using click events on dynamically added DOM elements

Question: Why does it work when element is hard-coded into HTML but does not work when added dynamically via Jquery?问题:为什么当元素被硬编码到 HTML 时它可以工作,但通过 Jquery 动态添加时却不起作用?

I am teaching my self Jquery within my self learning of javascript, and I am just creating a simple troubleshooting assistant app for the sake of learning.我正在自学 javascript 中自学 Jquery,我只是为了学习而创建一个简单的故障排除助手应用程序。 I actually have my code posted here: https://repl.it/@jllrk1/OrganicBothRay .我实际上在这里发布了我的代码: https : //repl.it/@jllrk1/OrganicBothRay

The way I have it set up so far is the user clicks on the header block to begin, which is set up with a onetime click function to create a UL for some products at my job in which we provide IT Service.到目前为止,我的设置方式是用户单击标题块开始,它设置了一次性单击功能,为我提供 IT 服务的工作中的某些产品创建 UL。

I then am trying to be able to click each product in that list to pull troubleshooting walkthroughs for that specific product (it will guide the user based on what they click or enter).然后,我尝试能够单击该列表中的每个产品以提取该特定产品的故障排除演练(它将根据用户单击或输入的内容引导用户)。

For testing purposes I just tried having the background of the list item in which is clicked to change to red.出于测试目的,我只是尝试将单击其中的列表项的背景更改为红色。

I cannot get this to work, or my console.log to fire telling me that the function is not getting called.我无法让它工作,或者我的 console.log 触发告诉我该函数没有被调用。

however, if I hard code in the ul into the html, using the same code for the click events, it works just fine.但是,如果我将 ul 中的硬编码到 html 中,对单击事件使用相同的代码,它就可以正常工作。

Am I doing something wrong?难道我做错了什么?

Just looking to gain a better understanding!只是想获得更好的理解!

$(function () {
//*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_
//set up variables
//*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_
//var $liItems = $('.product li');
var $chief = $(".chiefblock");
var $container = $("#container");
var $this = $(this);
var $error = '';



var initList = function () {
  console.log("initList initiated");

        $container.append('<div class="product"><ul><li>TASC</li><li>TABE</li><li>Las Links</li><li>TerraNova</li><li>E-Direct</li></ul></div>'); 
    $("p").text("Start by selecting a product");

}


var navItems = function (event){
      console.log("navItems initiated");
      var target = $(event.target);
          if (target.is("li") ) {
            target.css("background-color", "red" );
          }



}       
var nObject = function () {
        $container.append('<div id = "tasc"><h2>Tasc</h2><p></p></div></div>');
    $('#newItem').prepend('<h2>Item</h2>');




}


$('.chiefblock').one('click', initList)
    //$('li').on('click', navItems) this i tried and does not work
$('#newObject').on('click', nObject)
$('ul').on('click', navItems)
//$liItems.on('click', navItems)this i tried and does not work


});

for dynamically added DOM elements use对于动态添加的 DOM 元素使用

$(document).on('click', '#element', function() {
    console.log($(this))
})

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

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