[英]replacing .innerText moves div position
When the function changeText() is called the the div postion moves down for some reason当调用 function changeText()时, div位置由于某种原因向下移动
JS: JS:
function changeText() {
document.getElementById("div1").innerText = "string"
}
HTML: HTML:
<div id="div1" onmouseover="changeText()">
<p>div 1</p>
</div>
You are replacing all the content of the div with text.您正在用文本替换 div 的所有内容。 So you are removing the paragraph element.
所以你正在删除段落元素。 If you replace the content of the paragraph element it will not shift since the element remains.
如果您替换段落元素的内容,它不会移动,因为该元素仍然存在。
function changeText() { document.querySelector("#div1 p").innerText = "string" }
<div id="div1" onmouseover="changeText()"> <p>div 1</p> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.