I tried to append several dynamic tables to a DIV container and found something interesting.

<div id="divContainer">
    <table></table>
</div>
<script type="text/javascript">
$(function() {
    var div = $("#divContainer");
    var table = div.find("table");
    var c = 0;
    for (var i = 0; i < 3; i++) 
    {
        table = table.clone();
        div.append(table);
    }
    alert(div.children().length);
    alert(div.html());
});
</script>

What will 'div.children().length' be? It should be 4 in this case, but you will get 3 in IE7 and 4 in Firefox 3. The 'alert(div.html())' provide more detail.  In IE7, the div.html() looks like this:

<TABLE jQuery1218765772477="227"><TBODY></TBODY></TABLE>
<TABLE jQuery1218765772477="228"><TBODY jQuery1218765772477="null"></TBODY></TABLE>
<TABLE jQuery1218765772477="null"><TBODY jQuery1218765772477="null"></TBODY></TABLE>
<TABLE jQuery1218765772477="null"> <TBODY jQuery1218765772477="null"></TBODY></TABLE>

And div.html() will return blow result in Firefox3:

<table></table><table></table><table></table><table></table>

It seemed that jQuery added additional identification information (jQuery1218765772477 attribute) in IE and some of them are 'null'.  After more experiments, I found if I modified the code to:

table2 = table.clone();
div.append(table2);

I got div.children().length==4 and

<TABLE jQuery1218765990513="231"><TBODY></TBODY></TABLE>
<TABLE jQuery1218765990513="232"><TBODY jQuery1218765990513="null"></TBODY></TABLE>
<TABLE jQuery1218765990513="233"><TBODY jQuery1218765990513="null"></TBODY></TABLE>
<TABLE jQuery1218765990513="234"><TBODY jQuery1218765990513="null"></TBODY></TABLE>

Every table got a unique jQuery1218765990513 attribute, so the jQuery.unique() didn't filter out the extra 'null' attribute table object .  I don't think "table = table.clone()" is illegal syntax and this should be a bug of jQuery.clone().

I just submitted this bug to http://dev.jquery.com/newticket.  If there is any update, I will post it on my blog.

【中文摘要】

明明在趕程式,被這個Bug絆住,又跟它耗了大半天。利用clone()的寫法在DIV中動態新增Table object,卻發現明明放了4個,children().length卻只傳回3,但這問題只發生在IE上,Firefox上是正常的。研究了一下,發現clone()在IE下會產生額外的Attribute識別,而使用table=table.clone()的寫法,會讓這些識別Attribute變成null,在算個數時就會被過濾掉,看來是jQuery.clone()的Bug,暫無氣力追進去修改,但我先把它Submit反應出去,如果有消息再跟大家說吧。


Comments

Be the first to post a comment

Post a comment