【问题标题】:IE9 isn't applying css to dynamically created elements using jQuery.afterIE9 没有将 css 应用于使用 jQuery.after 动态创建的元素
【发布时间】:2013-09-18 18:51:24
【问题描述】:

我在表格顶部添加了一个 div 元素,其中包含当用户单击某些文本时通过 ajax 获得的一些信息。我需要在用户点击的记录后直接显示信息。它在内部使用,所以我只需要支持 chrome 和 IE9。 Chrome 正在做它需要做的事情,但 IE9 不喜欢它。

我使用 jQuery 的 .after 函数将 div 直接放在用户单击的表格行之后。两种浏览器都将 div 放在它应该去的地方,但 IE9 不会将任何 css 应用于新元素。

 <table>
   <tr><td>blahblah1</td><td class="secondRow">secondRow1</td><td class="thirdRow">thirdrow1</td></tr>
   <tr><td>blahblah2</td><td class="secondRow">secondRow2</td><td class="thirdRow">thirdrow2</td></tr>
   <tr><td>blahblah3</td><td class="secondRow">secondRow3</td><td class="thirdRow">thirdrow3</td></tr>
   <tr><td>blahblah4</td><td class="secondRow">secondRow4</td><td class="thirdRow">thirdrow4</td></tr>
   <div class="ajaxedInfo">control group</div>
   <tr><td>blahblah5</td><td class="secondRow">secondRow5</td><td class="thirdRow">thirdrow5</td></tr>
</table>

$('.secondRow').click(function(){
   $('.ajaxedInfo').remove();
   $('.inlineCSS').remove();
   $(this).parent().after('<div class="ajaxedInfo">ajaxed info coming in about</div>');
});

$('.thirdRow').click(function(){
   $('.ajaxedInfo').remove();
   $('.inlineCSS').remove();
   $(this).parent().after('<div class="inlineCSS" style="position:absolute;background:#c6c6c6;">inline css doesn\'t work either</div>');
});

.secondRow
{
cursor:pointer;
border:1px solid black;
}
.thirdRow
{
cursor:pointer;
}
.ajaxedInfo
{
position:absolute;
background:#cccccc;
width:300px;
text-align:center;
border:1px solid black;
}

这是我一直在玩的一个小提琴,它演示了 http://jsfiddle.net/QyUwA/4/ 这将按照我想要的方式在 Chrome 中工作,而不是 IE9

我环顾四周,似乎 IE7 做类似的事情有很多问题。人们建议重新绘制元素。我试过隐藏,然后在添加 div 后显示它,但这没有帮助。

【问题讨论】:

  • 您的小提琴在 IE9 中对我有用...您确定这与您将 div 附加到表的事实无关(这是无效的?)
  • 如果您的小提琴在 IE9 中为 @KevinB 工作,也许您将 IE9 设置为 IE7 模式。
  • 我不知道它是无效的,但这是有道理的,它会是无效的。有趣的是它适用于 IE9

标签: jquery css dynamic internet-explorer-9 insertafter


【解决方案1】:

改用表格行。

像这样:

$(this).parent().after('<tr class="ajaxedInfo"><td colspan="3">ajaxed info coming in about</td></tr>');

http://jsfiddle.net/QyUwA/4/

--用例子编辑。

【讨论】:

  • @A.Wolff 对此感到抱歉。我是新来的。我添加了一个我在他的 jfiddle 中测试过的示例,它可以工作。
  • 在小提琴中,如果您更改某些内容并单击顶部的更新,它将为您提供一个新的 url,基本上将 4 更改为下一个可用的数字。看起来我将添加一个 tr 而不是 div。谢谢,也感谢@Kevin B
  • 谢谢@Kevin。添加表格行也效果更好,因为它不覆盖下面的行,就像您在 Chrome 中为我所做的那样。不过,您可能需要调整 CSS 位置。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-09-13
  • 2011-02-24
  • 2014-03-09
  • 2018-08-07
  • 1970-01-01
  • 1970-01-01
  • 2017-07-14
相关资源
最近更新 更多