动态更改 DOM 元素边框时的 Webkit 回流问题

发布于 2024-09-13 01:47:27 字数 389 浏览 9 评论 0原文

我有两个 div 元素,其中一个元素环绕另一个元素。然后我使用 JavaScript 在运行时向外部 div 添加边框。不知何故,当外边框发生变化时,基于 webkit 的浏览器不会进行重排。最终结果看起来很难看——内部 div 溢出了外部。 HTML 代码如下: [div id="outer][div id="inner" style="border: 纯蓝色; height: 50px;"][/div][/div]

事件处理程序也很简单: document.getElementById("outer").setAttribute("style", "border: Solid green")

当我发现这一点时我简直不敢相信,因为这是一个微不足道的回流任务。或者我错过了什么?有谁遇到类似的问题,有什么解决方法吗?谢谢。

I have two div elements where one wraps around the other. Then I use JavaScript to add border to the outer div in runtime. Somehow webkit based browser doesn't do a reflow when the outer border is changed. The end result looks ugly - the inner div overflows the outer. Here is the HTML code:
[div id="outer][div id="inner" style="border: solid blue; height: 50px;"][/div][/div]

Event handler is simple as well:
document.getElementById("outer").setAttribute("style", "border: solid green")

I couldn't believe it when I found this out since it's such a trivial reflow task. Or did I missing anything? Does anyone encounter similar problem, what's the workaround? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

当爱已成负担 2024-09-20 01:47:27

border: Solid green 不声明任何尺寸,例如:"border:1px Solid green"

我认为重置不安全整个 style 属性,因为您可能会弄乱其他一些东西。尝试向className添加一个类,这样会更简洁一些。

border: solid green Doesn't state any size, eg: "border:1px solid green"

I don't think it's safe to reset the entire style attribute because you might mess up some other things. Try adding a class to className, which is a bit cleaner.

囚你心 2024-09-20 01:47:27

如果将外部和内部设置为 float:left 它将按计划工作。然而,您要么需要给它一个固定的宽度,要么在 div(内部)内包含内容。

#outer { float: left;}
#inner { float: left;}

[div id="outer"][div id="inner"]文本[/div][/div]

If you set the outer and inner to float:left it will work as planned. However, you will either need to give it a fixed width or have content inside the div (inner).

#outer { float: left;}
#inner { float: left;}

[div id="outer"][div id="inner"]Text[/div][/div]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文