简体   繁体   English

在javascript或jquery中将html元素添加到动态创建的div中

[英]Add html element to dynamically created div in javascript or jquery

I have created dynamic divs through the javascript in my asp.net mvc application. 我已经通过asp.net mvc应用程序中的javascript创建了动态div。 This is created in a for loop and I keep each id as an index value: 这是在for循环中创建的,我将每个id保留为索引值:

 for (j = 1; j <= count; j++) {
    $("<div id=" + j + "> Step " + j +  "</div>").
        hide().appendTo("#parentDiv").fadeIn();
 }

Now, I want to add new elements to these divs, within the same for loop. 现在,我想在相同的for循环中向这些div添加新元素。 But when I am going to attach it, I am not getting the current indexed div, so that I can append it. 但是,当我要附加它时,我没有得到当前的索引div,因此可以附加它。 In short, I want to render html elements dynamically in for loop on dynamically created div elements within same loop. 简而言之,我想在同一循环内动态创建的div元素上的for循环中动态呈现html元素。 How do I achieve this? 我该如何实现? Edited: see the real scenario is this. 编辑:看到的真实情况是这样。 how to achieve? 怎么实现?

 for (j = 1; j <= count; j++) {
    $("<div id=" + j + "> Step " + j +  "</div>").hide().appendTo("#parentDiv").fadeIn();
                    for (i = 1; i <= count; i++) {
                          $("<div id=" + i + "> Data of column " + i +  "</div>").
                         hide().appendTo("#"+j).fadeIn();
                        }

 }

First you shouldn't assign just numbers to your ID, valid IDs begin with an alpha character. 首先,您不应该只为ID分配数字,有效的ID以字母字符开头。 You should be able to reference these new objects using their respective ID later in your script. 您稍后应该可以在脚本中使用它们的相应ID来引用这些新对象。 The rest of your question is cryptic... 您剩下的问题都是神秘的...

Be careful if you are trying to attach events to these, you have to use the live() method. 如果尝试将事件附加到这些事件上,请务必使用live()方法。 Classic bind() or its shortcuts click(), hover(), and so on won't work on dynamicaly added elements. 经典的bind()或其快捷方式click(),hover()等在动态添加的元素上不起作用。

HTH 高温超导

<script src="jquery.js"></script>
<style>
#hello{
    height: 100px;
    width: 500px; 
    background: blue;
}

.small{
    margin-top: 10px;
    height: 60px;
    width: 500px;
    background: red;
}

.smallest{

    margin-top: 10px;
    height: 10px;
    width: 500px;
    background: green;

}
</style>
<script>
$(function(){
for (j = 1; j <= 5; j++) {

    var div = $("<div></div>").attr({"id": "div"+j , 'class': 'small'}).appendTo($('#hello'));
    //create some variable and add to var div
    $('<div></div>').attr({"class": 'smallest'}).appendTo(div);
    $('<div></div>').attr({"class": 'smallest'}).appendTo(div);
    $('<div></div>').attr({"class": 'smallest'}).appendTo(div);
    }




});
</script>
<div id="hello"></div>

您可能需要看一下: http : //api.jquery.com/live/

this would work. 这会工作。

var count = 10
for (j = 1; j <= count; j++) {
    $("<div id=a" + j + "> Step " + j + "</div>").hide().appendTo("#parentDiv").fadeIn();
    $('#a' + j).html("test" + j);
}

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

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