Putting <div> inside <td> dynamically using Jquery (vai xml parsing)

为君一笑 提交于 2019-12-11 09:48:56

问题


i need to get id of div which i am creating inside a my code is:

    tab_Div = $ ('#id_of_table')
    $(xml).find('something').each(function(i){
    value1 = $(this).attr('some_attribute');
    newID = "divID_"+i;

    var my_row = $('<tr><td>'+value1+'</td><td><div id="'+newID+'"></div></td></tr>').appendTo(tab_Div);

    drawBar(value1,newID);
    //This function draws progressbar of "value1" in the div whose id is "newID"
});

theoretically, this function should add a row to a table having two cells 1. which display value 2. progressbar of that value.

but i think , it has a problem with 'id' of 'Div'. and "newID" is a string not 'id' of any obj

can anybody help me on this ???
pls tell me how can i draw desired table (i.e. with progressbar in one cell)

code of drawBar()

function drawBar(no,eid)
    {
        $(eid).progressbar({value: no});
        $(eid).css({background: '#99FF66'});
        $(eid+" > div").css({background: '#009900'});
        $(eid+" > div").css({border: 'none'});
    }

drawbar function just draws a jqueryUI progress bar into DIv which it receives at "eid"


回答1:


hi i have seen your code and found that you have not closed the div tag inside your td EDIT: not only the div tag is not closed but u have also passed wrong variable of table tab_Div you have passed tab_div

  tab_Div = $ ('#id_of_table')
    $(xml).find('something').each(function(i){
    value1 = $(this).attr('some_attribute');
    newID = "divID_"+i;

    var my_row = $('<tr><td>'+value1+'</td><td><div id="'+newID+'" ></div></td></tr>').appendTo(tab_Div);

    drawBar(value1,newID);

EDIT AFTER THE COMMENT:

FOLLOWING IS THE CODE:

<body>
<table id="id_of_table" style="width:50%; border:1px solid red; float:left" >
</table>
<script type="text/javascript" >
$(document).ready(function(){

    tab_Div = $ ('#id_of_table');
    for (var i =1; i<=10; i++){
        value1 = "test";
        newID = "divID_"+i;
        $('<tr><td>'+value1+'</td><td><div id="'+newID+'" >i m inside div </div></td></tr>').appendTo(tab_Div);
}
    //drawBar(value1,newID);


});


</script>
</body>

The above code work fine, when u are appending it to table why are you passing it to the variable my_row not required. try my code in HTML page, remember to add jQuery file.




回答2:


i think this code is wrong

<div id="'+newID+'"</div>

please change it to

<div id='+newID+'></div>



回答3:


the easiest way i've found to generate a unique enough id in javascript is to get properties from a Date object.

var date = new Date();
var id = "" + date.getHour() + date.getMinutes() + date.getSeconds() + date.getMilliseconds();

to track IDs that have been created you can use an array

var _ids= new Array();

...

$(xml).each(function(i) {
  ...
  newID = "divID_"+i;
  _ids.push(newID);
});

then, when you need to access the divs you can just go through the array



来源:https://stackoverflow.com/questions/8586846/putting-div-inside-td-dynamically-using-jquery-vai-xml-parsing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!