jQuery append()在IE中不起作用

时间:2011-06-30 12:03:27

标签: javascript jquery

我正在尝试使用以下内容将以下表格在字符串中添加到我的页面上的div:

var table = 
'<table data-custom="3963770" id="table" cellpadding="3" cellspacing="5" 
        valign="top" width="995px" border="3" bordercolor="#83725B">

        <th height="50" colspan="2">Company Name</th>
        <th height="50" colspan="3">Esco Number</th>
        <th height="50" colspan="1">Province</th>
        <th height="50">Sector</th>
        <th height="50">Technology</th>
        <th height="50" colspan="2">Status</th>
      <tr>
        <td colspan="2">3H Envirostrategies cc</td>
        <td colspan="3">11059420</td>
        <td>Gauteng, KZN, Western Cape, Mpumalanga, Free State, 
            Eastern Cape</td>
        <td>
          <select id="mainSectors0">
            <option>Commercial</option>
          </select>
        </td>
        <td>
          <select id="mainTechs0">
            <option>Project Management</option>
          </select>
        </td>
        <td colspan="2">Active</td>
      </tr>
      <tr>
        <td style="border: none;" colspan="5">
          <div data-custom="contact_info" style="display:inline;"><u>Contact information</u></div>
        </td>
      </tr>
      <tbody data-custom="place_holder">
      </tbody>
    </table>';

我有一个div标签:

<div id="table"></div>

然后我尝试使用它将表添加到div:

$(table).appendTo($('#table'));

// I've tried $("#table").append(table); but no luck.

除了IE 6+之外,它在其他所有浏览器中都能正常工作。有没有人知道解决方法,或者我做错了什么?

提前致谢。

5 个答案:

答案 0 :(得分:3)

您的代码也适用于我,也适用于IE:http://jsfiddle.net/doktormolle/N5z5T/

您确定在table = '<table>....</table';

之前使用var-keyword

如果没有,这可能会破坏IE,请参阅以下相关主题:jQuery selector does not work in IE7/8

答案 1 :(得分:1)

table变量中的HTML无效。尝试制作$('#table').append('<div>a</div>');。它适用于IE。使您的HTAMl有效。

答案 2 :(得分:0)

使用appentTo时,不需要将div作为jquery对象。尝试休息

 $(table).appendTo('#table'); 

$('#table').append(table)

答案 3 :(得分:0)

您是否有多个具有相同ID“table”的元素?

答案 4 :(得分:-1)

试试这个:

$('#table').html(table);

它使用jQuery's html function,这在这种情况下比追加更有意义。不知道为什么追加不起作用

相关问题