简体   繁体   English

如何替换HTML元素,同时保持页面上所有其他元素的位置相同

[英]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:inlinedisplay: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.

相关问题 如何在同一页面中呈现不同的元素,以保持 React js 中的所有其他内容相同? - How to render different element in the same page keeping all other things same in react js? 如何删除HTML表单中最后一个元素以外的所有元素 - How to remove all the elements other than the last element in a HTML form 在 HTML 页面中获取所有带有 `position:fixed` 的元素? - Get all elements with `position:fixed` in an HTML page? 如何固定位置元素,以便将鼠标悬停在一个元素上时,其他两个元素应保留在同一位置 - How fix position element so that while hovering on one element other two element should remain at same place 保留一些html元素而不替换新的相同html元素 - retain some html elements without replace new same html element 始终将元素放置在页面底部,其他元素下方 - Position an element always at the bottom of the page, below other elements 如何在每次页面重新加载时更改渐变背景,同时保持光标位置元素处于活动状态? - How do I make the gradient background change each time page has reload while also keeping the cursor position element active? 如何用jQuery中的相同元素替换多个元素? - How to replace multiple elements with the same element in jQuery? 在相同的 html DOM 元素中替换后转换字符串 position - Convert string after replace in html DOM element in the same position 如何替换所有最后的 html 元素标签 - How to replace all last html elements tags
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM