简体   繁体   English

如何动态生成元素

[英]How to to dynamically generated elements

I use the jQuery plugin dForm to dynamically generate form fields. 我使用jQuery插件dForm动态生成表单字段。

For events, I know I can access those fields with 对于事件,我知道我可以使用

$(document).on("click","#id", function(){});

But if I want to, say 但是如果我想说

$("#id").after("<div></div>");

Then how do I do that? 那我该怎么办呢? I've tried 我试过了

$("#id").after("<div></div>").trigger("create");

Even though I don't really understand what it does, to no avail. 即使我不太了解它的作用,也无济于事。

Thanks. 谢谢。

I believe there's some confusion on what the event delegation is actually doing. 我相信活动代表团实际上在做什么。 The reason you're using .on() is because the event handler is being processed at DOM ready but your dynamic element doesn't exist. 您使用.on()的原因是因为事件处理程序已在DOM准备就绪时进行处理,但您的动态元素不存在。 Now, when you're going to append to dynamic content, there's no need for event delegation, since when you run the append command, you should be targeting an element that exists on the page, whether it be dynamically or there from the start. 现在,当您要追加到动态内容时,不需要事件委托,因为当您运行append命令时,您应该针对页面上存在的元素,无论它是动态的还是从一开始就存在。

So, say you create a div dynamically with the ID of test . 因此,假设您使用test的ID动态创建div。 You can append to this div with a simple $("#test").append("<span>see, new span</span>"); 您可以在该div后面附加一个简单的$("#test").append("<span>see, new span</span>"); , since by the time you run this command, the element exists. ,因为在您运行此命令时,该元素已存在。 I hope this helps. 我希望这有帮助。

This works because it's responding to an event : 之所以有效,是因为它正在响应事件

$(document).on('click', '#id', function(){});

The reason is because events traverse the entire DOM structure, from the specific element all the way up to the parent document/window. 原因是因为事件遍历整个DOM结构,从特定元素一直到父文档/窗口。 So in this case jQuery assigns the event listener to the document instead of the element, and then filters events by the selector. 因此,在这种情况下,jQuery将事件侦听器分配给document而不是元素,然后由选择器过滤事件。 So even if elements are dynamically added after the fact, they'll still be caught because they still pass their events up to document . 因此,即使在事后动态添加元素,它们仍然会被捕获,因为它们仍会将事件传递给document

This, however, isn't responding to an event: 但是,这不响应事件:

$('#id').after('<div></div>');

This is just selecting an element, performing an action, and then it's done. 这只是选择一个元素,执行一个动作,然后完成。 So if that element isn't on the DOM, that action isn't performed. 因此,如果该元素不在DOM上,则不会执行该操作。

There's really no way around this. 真的没有办法解决这个问题。 If you want every $('#id') to append a '<div></div>' then any time you remove the id element and re-add it, you need to call that line of code again. 如果要每个$('#id')附加一个'<div></div>'则每次删除id元素并重新添加它时,都需要再次调用该行代码。

Your question is not clear enough but only from a guess I think you want to dynamically add a div and also want to register an event for that div , if this is the case then you may do it like this way too, but there are other ways as well. 您的问题还不够清楚,但仅凭猜测,我认为您想动态添加div并还想为该div注册一个event ,如果是这种情况,那么您也可以像这样进行操作,但是还有其他方法方式也是如此。

$("#id").after($("<div/>", {
    'id':'test',
    'click':function() { alert ("Clicked!"); }
}));

DEMO. 演示。

Another approach to do this (Keep the event handler registered like this using #test ) 这样做的另一种方法(使事件处理程序使用#test保持这样注册)

$(document).on("click", "#test", function(){
    alert ("Clicked!");
});

Insert the new div 插入新的div

$("#id").after($("<div/>", { 'id':'test'}));

DEMO. 演示。

Also, if you want to dynamically create/insert a new div and want to trigger the click event on this new div just right after you insert it, then you may try this 另外,如果您要动态创建/插入新的div并想在插入后立即在此新的div上触发click事件,则可以尝试执行此操作

$(function(){
    $(document).on("click","#test", function(){
        alert ("Clicked!");
    });

    // insert a new div with id 'test' and invoke click event immediately
    var div = $("<div/>", { 'id':'test'});
    $("#id").after(div);
    div.trigger('click');
});

DEMO. 演示。

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

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