Javascript jQuery append() 在 IE 中不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6534431/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 22:10:55  来源:igfitidea点击:

jQuery append() not working in IE

javascriptjquery

提问by MattBH

I'm trying to append the following table built up in a string to a div on my page using this:

我正在尝试使用以下方法将在字符串中构建的下表附加到我页面上的 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>';

I have a div tag with:

我有一个 div 标签:

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

I then try to use this to add the table to the div:

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

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

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

It works fine in every other browser except IE 6+. Does anybody know of a workaround, or if i'm doing something wrong?

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

Thanks in advance.

提前致谢。

采纳答案by Dr.Molle

Your code works for me, also on IE : http://jsfiddle.net/doktormolle/N5z5T/

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

Are you sure that you use the var-keyword before table = '<table>....</table';

你确定你之前用过var-keyword吗 table = '<table>....</table';

If not, this may break IE, see this related topic: jQuery selector does not work in IE7/8

如果没有,这可能会破坏 IE,请参阅此相关主题:jQuery 选择器在 IE7/8 中不起作用

回答by Alex Pliutau

Your HTML in tablevariable is invalid. Try to make $('#table').append('<div>a</div>');. It works in IE. Make your HTAMl valid.

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

回答by zatatatata

Could it be that you have multiple elements with the same id "table"?

可能是您有多个具有相同 id“table”的元素吗?

回答by Spycho

Try this:

尝试这个:

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

It uses jQuery's html function, which makes a bit more sense in this circumstance than append. Not sure why append isn't working though

它使用jQuery 的 html 函数,在这种情况下它比 append 更有意义。不知道为什么 append 不起作用

回答by Vivek

you don't need to make div as a jquery object when you are using appentTo. try fallowing

当您使用appentTo. 尝试休耕

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

or

或者

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