[英]How to replace an HTML element while keeping the position of all other elements on the page the same
Let's say you're on a third party webpage and so you have no control over the page, except for your javascript. 假设您在第三方网页上,因此除了JavaScript之外,您无法控制该网页。
You want to replace an element with an empty DOM node, but you want the page to maintain the same positioning for all other elements on the page. 您想用一个空的DOM节点替换一个元素,但是您希望页面为页面上的所有其他元素保持相同的位置。 You don't know what the element will be and what the styles are on the webpage. 您不知道元素将是什么以及网页上的样式是什么。
If you replace the element with a DIV, and you manually set the width and the height of that div to be the same as the replaced element, then you are almost there. 如果用DIV替换元素,然后手动将该div的宽度和高度设置为与被替换的元素相同,那么您几乎就在那。 However, you would not know if there are any other styling on the page that would apply to the DIV and mess things up. 但是,您将不知道页面上是否还有其他样式可应用于DIV并弄乱了样式。 Also, if the replaced element was floated to the left or right, or was display:inline
, then replacing it with a DIV might mess up the position of other elements on the page. 另外,如果替换的元素浮动到左侧或右侧,或者display:inline
为display:inline
,那么用DIV替换它可能会弄乱页面上其他元素的位置。
Is there a foolproof way to do this? 有一种万无一失的方法吗?
Instead of replacing the whole element, you could just throw away its content. 除了替换整个元素,您还可以丢弃其内容。 However, it could still have borders and background images, not to mention event handlers like mouseover, so there are plenty of awkward cases. 但是,它仍然可能具有边框和背景图像,更不用说像mouseover这样的事件处理程序了,因此有很多尴尬的情况。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.