简体   繁体   English

如何在jQuery上选择动态创建的对象?

[英]How do I select a dynamically created object on jQuery?

//jQuery Code    
$(".content-update").click(function(){
    var contentClass = $(this).attr("href").substring(1);
    var content = $("." + contentClass).html();
    $("#stuff").html(content);
});


$(document).on('click', '#'+contentClass, function(){
    alert("Hi!");
});

//HTML Code
<div class="hiddenContent Wolf"><p><img src="Wolf.png" id="#Wolf"/></p> </div>

The content class here is the class name and ID for several HTML content. 这里的内容类是几个HTML内容的类名和ID。 I'm trying to create a picture after clicking on a link and after the picture is clicked, I'm trying to produce a sound from it. 我尝试在单击链接后创建图片,然后单击图片,然后尝试从中产生声音。 So far, I'm able to make only the picture appear. 到目前为止,我只能使图片出现。 Nothing happens on clicking the picture. 单击图片没有任何反应。 Here is the link. 链接在这里。 http://www.theindependentwolf.com/HowlFarm/demos/ http://www.theindependentwolf.com/HowlFarm/demos/

I have added only the wolf picture so far for testing purposes. 到目前为止,我仅添加了狼图片用于测试目的。 Thanks. 谢谢。

Uncaught ReferenceError: contentClass is not defined 未捕获ReferenceError:未定义contentClass

You have contentClass as local variable and you trying to access it outside its local scope. 您将contentClass作为局部变量,并且尝试在其局部范围之外访问它。

Try below as per given limited code, but could be improved. 请按照给定的受限代码尝试以下操作,但可以进行改进。

$(".content-update").click(function(){
    var contentClass = $(this).attr("href").substring(1);
    var content = $("." + contentClass).html();
    $("#stuff").html(content);
    $("#stuff").on('click', '#'+contentClass ,function(){
         alert("Hi!");
    });
});

And also change id 并更改ID

<div class="hiddenContent Wolf"><p><img src="Wolf.png" id="Wolf"/></p> </div>

maybe this will help 也许这会有所帮助

$(".content-update").click(function(){
    var contentClass = $(this).attr("href").substring(1);
    var content = $("." + contentClass).html();
    $("#stuff").html(content);
    //lets move your contentClass click hendler here
    //also you trying add hendler to id not to class ... so "'.' + contentClass" should be used instead
    $(document).on('click', '.'+contentClass, function(){
        alert("Hi!");
  });
});

Why? 为什么? because the var is bind to function where it is created. 因为var绑定到创建它的函数。 Its not visible outside this scope 在此范围之外不可见

contentClass is in two separate scopes. contentClass在两个单独的范围内。 Since you are generating the content dynamically your only option is to add the second event handler inside the callback of the first event handlers. 由于您是动态生成内容的,因此唯一的选择是将第二个事件处理程序添加到第一个事件处理程序的回调中。 To clearly differentiate the scopes: 为了清楚地区分:

1. global function scope    

//jQuery Code    
$(".content-update").click(function(){
-----------------------------------------------------------
 2. function scope 
 var contentClass = $(this).attr("href").substring(1);
 var content = $("." + contentClass).html();
 $("#stuff").html(content);
-----------------------------------------------------------
});


$(document).on('click', '#'+contentClass, function(){
 ----------------------------------------------------------
 3. function scope
 alert("Hi!");
 ----------------------------------------------------------
});

Tip: Each scope is like a private namespace. 提示:每个作用域就像一个私有名称空间。 Scope 2 and 3 has access to the global scope but not to each other. 范围2和3可以访问全局范围,但不能相互访问。 And since you declared your contentclass inside your second scope, contentclass in your global scope doesn't exist and will always be undefined. 并且由于您在第二个作用域内声明了内容类,因此全局范围内的内容类不存在,并且始终是未定义的。 Of course with this said you might be tempted to declare your contentclass outside, it would work but its not good practise cause you can't predict when your dynamic content will be generated inside your call back and can lead to buggy code. 当然,这样说可能会诱使您在外部声明contentclass,它会起作用,但是它不是一个好习惯,因为您无法预测何时在回调内生成动态内容并导致错误代码。 Best practise is to add the second event handler into the callback of the first event handler. 最佳实践是将第二个事件处理程序添加到第一个事件处理程序的回调中。

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

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