关于js中 document.body.scrollTop 不能返回正确值的原因

本来是为了通过document.body.scrollTop来获取浏览器垂直滚动条向下滚动的像素,但是不管滚动条在什么位置总是返回是0,造成这样的原因和html的头部声明有关,如果头部声明 为:<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">,这样肯定得到的结果是 0,如果该为<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">就能得到正确的结果,对于第一种头部声明想要得滚动条的滚动的高度怎么办,解决方法有两种

1、var scrollPos; if (typeof window.pageYOffset != ‘undefined‘) {    scrollPos = window.pageYOffset; } else if (typeof document.compatMode != ‘undefined‘ &&      document.compatMode != ‘BackCompat‘) {    scrollPos = document.documentElement.scrollTop; } else if (typeof document.body != ‘undefined‘) {    scrollPos = document.body.scrollTop; }

2、用document.documentElement.scrollTop 替代 document.body.scrollTop

转自:http://my.csdn.net/netingcn

时间: 2024-08-05 18:17:13

关于js中 document.body.scrollTop 不能返回正确值的原因的相关文章

JS中 document.getElementById 对象

Document 对象 每个载入浏览器的 HTML 文档都会成为 Document 对象. Document 对象使我们可以从脚本中对 HTML 页面中的所有元素进行访问. 提示:Document 对象是 Window 对象的一部分,可通过 window.document 属性对其进行访问. Document 对象集合 集合 描述 all[] 提供对文档中所有 HTML 元素的访问. anchors[] 返回对文档中所有 Anchor 对象的引用. applets 返回对文档中所有 Applet

bobo JS中document.write(&quot; &quot;);

<script>document.write("<h1>This is a heading</h1>");document.write("<p>This is a paragraph.</p>");</script> 您只能在 HTML 输出流中使用 <strong>document.write</strong>. 如果您在文档已加载后使用它(比如在函数中),会覆盖整个文

怎么在js中,访问viewbag,viewdata等等的值

在js中要访问viewbag,viewdata存储的值, var ss='@ViewBag.name'; 一定要加引号,单双随便,还有, ViewBag一定要写规范,不然会编译错误! 成功者的秘诀就是:我们应该学习水的精神,当时机不到的时候,把自己的能量储蓄起来,当时机来临的时候就能冲破障碍,奔腾入海,成就自己的梦想.

JS中document.createElement()用法及注意事项

今天处理了一个日期选择器的ie和ff的兼容问题,本来这种情况就很难找错误,找了好久才把错误定位到js中创建元素的方法document.createElement(),这个方法在ie下支持这样创建元素 var inputObj    = document.createElement     ("<input type='text' size='8' style='border:0px;border-bottom:2px solid #c0c0c0;'" readonly >&

js中document的用法

document.title //设置文档标题等价于HTML的title标签document.bgColor //设置页面背景色document.fgColor //设置前景色(文本颜色)document.linkColor //未点击过的链接颜色document.alinkColor //激活链接(焦点在此链接上)的颜色document.vlinkColor //已点击过的链接颜色document.URL //设置URL属性从而在同一窗口打开另一网页document.fileCreatedDa

js中document的用法小结(一)

document常用属性: document.title//设置文档标题,与HTNL中的title标签等价 document.bgColor//设置页面背景颜色 document.fgColor//设置页面前景色 document.fileCreateDate//文件建立日期,只读属性 document.fileModifiedDate//文件修改日期,只读属性 document.URL//可返回当前文档的URL document.linkColor//未点击过的链接颜色 doucment.al

JS 中document.URL 和 window.location.href 的区别

实际上,document 和 window 这两个对象的区别已经包含了这个问题的答案. document 表示的是一个文档对象,window 表示一个窗口对象. 一个窗口下面可以有很多的document对象.每个document 都有 一个URL. 但是,这不是所有的区别.当你ctrl + F5 一个链接 http://yourhost.com/#fragment 打印 alert(document.URL ); 和 alert(window.location.href); 发现,这两个的值不一

JS中document属性

document.title //设置文档标题等价于HTML的title标签 document.bgColor //设置页面背景色 document.fgColor //设置前景色(文本颜色) document.linkColor //未点击过的链接颜色 document.alinkColor //激活链接(焦点在此链接上)的颜色 document.vlinkColor //已点击过的链接颜色 document.URL //设置URL属性从而在同一窗口打开另一网页 document.fileCr

在js文件中写el表达式取不到值的原因及解决方法

1.javascript是客户端执行,EL是在服务端执行,而服务端比客户端先执行,所以取不到值 2.要想获取"${jcDropClass.jcClass.id}"的值,可以在jsp中,用一个全局变量接收,然后再js中使用 3.注意:在使用时,注意要添加双引号,如var jcClassId="${jcDropClass.jcClass.id}"; $(document).ready(function() { var schoolId=$('#jcSchoolSelec