示例(适用于Firefox): http://progamonth.com/files/tablestestfile.html
我无法将select元素动态添加到表中。这适用于Firefox,但在IE和Opera中失败。这是怎么回事?
DOM代码:
<table id = "myTable">
<thead>
<tr><th>1</th><th>2</th><th>3</th>
</thead>
<tbody>
</tbody>
</table>
<select id = "select1" class = "hidden">
<option>1</option>
<option>2</option>
<option>3</option>
</select>
<select id = "select2" class = "hidden">
<option>A</option>
<option>B</option>
<option>C</option>
</select>
Javascript:
$(document).ready(function(){
$('#myTable tbody').append(generate());
});
function generate()
{
var $row = $('<tr>');
var selects = [$('#select1'),
null,
$('#select2')];
for( var i in selects )
{
var $td = $('<td></td>');
if( selects[i] != null )
{
var $select = selects[i].clone().show().removeAttr("id");
$select.find('option:first').before($('<option>'));
$select.val("");
$td.append($select);
}
$row.append($td);
}
return $row;
}
答案 0 :(得分:1)
我刚试过你的代码。看来,在IE中,设置在'hidden'css class(display:none)中的display属性不会被覆盖,尽管你已经调用了show()。删除克隆选择元素的css类或将'display'属性显式设置为'block'/'inline'可以起作用:
$select.removeClass('hidden');
(或)
$select.css({display:'block'});