本文介绍了Javascript和Doctype在Firefox的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  function pageWidth(){
return(window.innerWidth!= null
?window.innerWidth
:(document.body!= null
?document.body.offsetWidth
:null
));


函数bodyloaded(){
winWidth = pageWidth();
window.scroll(0,0);
scrAmount = Math.floor(
((document.body ['scrollWidth'] - document.body.offsetWidth)/ 2)+ 8
);
scrollBy(scrAmount,0);
}

并应用于onload和onresize方法中的body标签,问题是if我把任何文档,这个代码不工作在Firefox上,但在IE浏览器上工作。
我调试scrollWidth和offsetWidth的值,并且总是得到相同的值,这使用Doctype发生。



任何解决方案?在怪癖模式(没有文档类型或怪癖模式doctype)中, document.body.scrollWidth / code>实际上在某些情况下返回文档的scrollWidth而不是body。在标准模式(大多数文档类型)中,它返回正文的scrollWidth,在某些情况下, document.documentElement.scrollWidth 返回文档的scrollWidth。请参阅为这个spec的草稿。


i have this javascript code :

function pageWidth() {
    return (window.innerWidth != null
        ? window.innerWidth
        : (document.body != null
            ? document.body.offsetWidth
            : null
    ));
}

function bodyloaded() {
    winWidth = pageWidth();
    window.scroll(0, 0);
    scrAmount = Math.floor(
        ((document.body['scrollWidth'] - document.body.offsetWidth)/2) + 8
    );
    scrollBy(scrAmount, 0);
}

and is applied on body tags in methods onload and onresize , the problem is if i put any doctype , this code doesnt work on firefox but works on IE.I debug the value of scrollWidth and offsetWidth , and allways get the same value , this happend using a Doctype.

Any solution?

解决方案

In quirks mode (no doctype or a quirks mode doctype), document.body.scrollWidth actually returns the scrollWidth of the document instead of the body in some cases. In standards mode (with most doctypes), it returns the scrollWidth of the body, and document.documentElement.scrollWidth returns the scrollWidth of the document in some cases. See http://dev.w3.org/csswg/cssom-view/#dom-element-scrollwidth for the spec draft for this.

这篇关于Javascript和Doctype在Firefox的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-18 16:26