jQuery 附加在 IE8 中不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7149338/
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-26 23:28:01  来源:igfitidea点击:

jQuery append not working in IE8

jqueryinternet-explorer-8

提问by Falk S.

 $('#buttonadd').click(function(){
            if ($counter < 10)
            {
                $counter++;
                $countonlyadd++;
                $('#buttondiv').append('<tr><td><select class="combo" name="combo'+$counter+'" style="width: 60px;" size="1"><option>UND</option><option>OHNE</option><option>ODER</option></select></td><td><input type="text" name="textbox'+$counter+'" class="textbox" value="" /><a id="removetxt" class="removetxt" style="text-decoration: none;line-height: 3;" href="#">&nbsp;[X]</a></td></tr>');
            }else{
            }
            $('#send_count').val($countonlyadd);
        });

My code is not working in Internet Explorer, and i dont know why. All other browsers are ok but IE is not. He is not adding even 1 textbox.

我的代码在 Internet Explorer 中不起作用,我不知道为什么。所有其他浏览器都可以,但 IE 不行。他甚至没有添加 1 个文本框。

采纳答案by zulucoda

ya the problem is IE compatibility mode. This works fine in IE9 http://jsfiddle.net/NP9pG/3/and firefox but when you switch to IE compatibility mode it doesn't work.

雅问题是IE兼容模式。这在 IE9 http://jsfiddle.net/NP9pG/3/和 firefox 中工作正常,但是当您切换到 IE 兼容模式时它不起作用。

<div id="buttondiv">  </div>
<div id="send_count"></div>
<input type="button" id="buttonadd" value="add" />

but this http://jsfiddle.net/NP9pG/4/will work fine tho in IE compatibility mode

但是这个http://jsfiddle.net/NP9pG/4/在 IE 兼容模式下可以正常工作

<table id="buttondiv">  </table>
<div id="send_count"></div>
<input type="button" id="buttonadd" value="add" />

ya the problem is your html mark-up as suggested make the following change

是的,问题是您的 html 标记按照建议进行以下更改

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

rather append items to tableinstead of divelement

而是将项目附加到table而不是div元素

therefore js code:

因此js代码:

$('#tableData').append('<tr><td><select class="combo" name="combo'+$counter+'"  style="width: 60px;"  size="1"><option>UND</option><option>OHNE</option><option>ODER</option></select></td><td><input  type="text" name="textbox'+$counter+'"  class="textbox" value="" /><a id="removetxt" class="removetxt"  style="text-decoration: none;line-height: 3;"  href="#">&nbsp;[X]</a></td></tr>');

hope that helps

希望有帮助

回答by ShankarSangoli

Looks like you are adding a trdirectly into which is not a valid html in any browser. Other browsers will not shout but IE will. Try this.

看起来您正在tr直接添加一个在任何浏览器中都不是有效 html 的内容。其他浏览器不会喊叫,但 IE 会。尝试这个。

Working demo

工作演示

Markup change

标记更改

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

JS change

JS变化

$('#buttonadd').click(function(){
            if ($counter < 10)
            {
                $counter++;
                $countonlyadd++;
                $('#buttondiv table').append('<tr><td><select class="combo" name="combo'+$counter+'" style="width: 60px;" size="1"><option>UND</option><option>OHNE</option><option>ODER</option></select></td><td><input type="text" name="textbox'+$counter+'" class="textbox" value="" /><a id="removetxt" class="removetxt" style="text-decoration: none;line-height: 3;" href="#">&nbsp;[X]</a></td></tr>');
            }else{
            }
            $('#send_count').val($countonlyadd);
        });

回答by Blazemonger

It appears you're appending a single TR to a div, which may be your problem. You should append it to the TBODY of a TABLE instead.

看来您将单个 TR 附加到 div,这可能是您的问题。您应该将其附加到 TABLE 的 TBODY 中。

回答by Bas Slagter

Maybe it helps if you create the elements first..so create an object for your row, column, textbox, etc. And than append that into each other. Finally append the row to your div. Besides that, it is a bit weird to add a row to a div, and not to a table...

如果您首先创建元素,也许会有所帮助……所以为您的行、列、文本框等创建一个对象。然后将其附加到彼此中。最后将该行附加到您的 div。除此之外,向 div 添加一行而不是向表添加一行有点奇怪......

回答by genesis

Questions are:

问题是:

  • Do you have $counter defined?
  • Do you have $countonlyadd defined?
  • Do you have <table> wrapper in #buttondiv ?
  • 你定义了 $counter 吗?
  • 你定义了 $countonlyadd 吗?
  • table#buttondiv 中是否有 < > 包装器?

Think about these questions and if result is "no", that's the problem

想想这些问题,如果结果是“否”,那就是问题所在

If you have <table> wrapper, change

如果您有 < table> 包装器,请更改

$('#buttondiv').

to

$('#buttondiv table').