简体   繁体   English

替换div的内容而不更改大小或滚动

[英]replace content of div without changing size or scrolling

I'm trying to write some code which replaces old content with a placeholder while new content is loaded (via ajax). 我正在尝试编写一些代码,以便在新内容加载时(通过ajax)用占位符替换旧内容。

<div id="target">
...
</div>

During the ajax request-response cycle, i'd like to replace the content of the target div with a 'loading' message or image. 在ajax请求-响应周期中,我想用“加载”消息或图像替换目标div的内容。

So far, this is simple ... 到目前为止,这很简单...

However in some cases the target is small, and others the target is large. 但是,在某些情况下,目标较小,而在其他情况下,目标较大。 In neither case do I want the target div's size to change or scrollbars to appear. 无论哪种情况,我都不希望目标div的大小改变或滚动条出现。 if the loading message or image needs to be clipped so be it. 如果加载消息或图像需要剪切,那就这样吧。

The signature (id, class, style) of the 'target' div must remain the same throughout the process and I cannot set a width+height on the target div. 在整个过程中,“目标” div的签名(id,类,样式)必须保持相同,并且我无法在目标div上设置width + height。

i do not want to rely on additional code being executed when the new content is received. 我不想依靠收到新内容时正在执行的其他代码。

how would you approach this, using basic javascript/dhtml and/or prototype-js only. 仅使用基本的javascript / dhtml和/或prototype-js,您将如何处理?

thanks, p. 谢谢,第

this is the best i can come up with for now: 这是我目前能想到的最好的方法:

function loading(id, content)
{
    var elem = $(id);
    if (!elem)
        return;

    var dim = elem.getDimensions();
    var width = dim.width - parseInt(elem.getStyle("padding-left")) - parseInt(elem.getStyle("padding-right"));
    var height = dim.height - parseInt(elem.getStyle("padding-top")) - parseInt(elem.getStyle("padding-bottom"));

    elem.innerHTML = "<div style=\"position:relative; overflow:hidden; padding:0; margin:0; border:0; width:" + width + "px; height:" + height + "px;\">" /*  */
            + content + "</div>";
}

feel free to post a better solution! 随时发布更好的解决方案!

// prototype
Object.prototype.updateFixedSizeHtml = function(html)
{
    this.innerHTML = "<div style='width:" + this.clientWidth + "px;height:" + this.clientHeight + "px;overflow:hidden;display:block;padding:0;border:0;margin:0'>" + html + "</div>";
}
// usage
document.getElementById('target').updateFixedSizeHtml('New Content');

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

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