简体   繁体   English

div上的jQuery .html('')移动整个页面

[英]jQuery .html('') on div moves entire page

I'm replacing the html in a div tag on my page on the keyup event of a textbox when the user enters a search. 当用户输入搜索内容时,我将替换文本框中键入事件的页面上div标签中的html。 When the html is set via jQuery's .html() method on the div tag the entire page shifts to the left and then back to the right. 当通过div标签上的jQuery的.html()方法设置html时,整个页面将向左移动,然后向右移动。 For some reason this is only happening when I deploy the website to an IIS site when I run locally in Visual Studio the page no longer shifts to the left and back to the right. 由于某些原因,这仅在将网站部署到IIS站点时才发生,而在Visual Studio中本地运行时,页面不再向左和向右移动。 When I replace the div tags content I am first clearing it by calling .html('') and then am setting it to the search results that is a ul tag. 当我替换div标签内容时,我首先通过调用.html('')清除它,然后将其设置为ul标签的搜索结果。 This issue is also only happening with Chrome it works fine on Edge. 此问题也仅在Chrome上发生,它在Edge上正常运行。

Posting code helps, so we do not have to make an assumption on what you are doing. 发布代码会有所帮助,因此我们不必假设您在做什么。

If you are in fact doing something like: 如果您实际上正在执行以下操作:

$("#id").html("");
$("#id").html("html-here");

Then that makes sense, the first .html() call sets the div to be empty, causing the shift you mentioned, then you fill up the div again on the next call to .html() . 那就有意义了,第一个.html()调用将div设置为空,导致您提到的移位,然后在下一次调用.html()再次填充div。

Using a single call instead will not cause the page to be re-rendered twice, but it will still overwrite the contents like you want (you may have been thinking of $.append() , where you do have to clear first: 相反,使用单个调用不会导致页面重新呈现两次,但仍会覆盖您想要的内容(您可能一直在考虑$.append() ,但必须先清除该内容:

$("#id").html("html-here");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM