将元素innerHTML替换为其自身的副本会更改文档高度

Replacing an elements innerHTML with a copy of itself changes the document height?

本文关键字:副本 高度 文档 innerHTML 元素 替换      更新时间:2023-09-26

我正试图修复iOS应用程序中的一个错误。我已经找到了一些在webView中运行的Javascript。

当我执行以下操作时:

 var innerHTML = anElement.innerHTML;
//in the full code I do other things here
//but the issue remains even with nothing here
 anElement.innerHTML = innerHTML; 

偶尔,上面代码片段开头和结尾的文档高度不相同;文档变小了,只有几个像素。

有人能解释为什么会发生这种情况吗?

通过添加CSS:似乎已修复

html {
    -webkit-text-size-adjust: none; /* Never autoresize text */
}

或者在obj-c中,使用将其应用于每一页

NSString* jsString =  @"var style = document.createElement('"style'"); document.head.appendChild(style); style.innerHTML = '"html{-webkit-text-size-adjust: none;}'"";
[self.webView stringByEvaluatingJavaScriptFromString:jsString]

(这显然是针对UIWebView的,我还需要写一些代码来确保它不会被添加两次)