按ID滚动div
我有一个具有这种结构的 HTML 页面。
<div class="scrollBoxYe">
<table class="grid">
<tr><td class="wd0"><div id="20110701" class="lcday"><div class="lcleft">Fri 01</div><div class="lcmid">The Sacred Heart of Jesus, solemnity - <a href="#" onclick="window.open('http://evangelizo.org/www/popup-saints.php?language=AM&id=10309&fd=0', '', 'width=500,height=450,top=120,left=120,scrollbars=yes')">St. Gal, Bishop (c. 489-553)</a>, <a href="#" onclick="window.open('http://evangelizo.org/www/popup-saints.php?language=AM&id=10480&fd=0', '', 'width=500,height=450,top=120,left=120,scrollbars=yes')">Bl. Antonio Rosmini, Priest, Founder of the Institute of Charity (1797-1855)</a></div><div class="lcright">Jul</div></div></td>
</tr>
<tr><td class="we0"><div id="20110702" class="lcday"><div class="lcleft">Sat 02</div><div class="lcmid">Immaculate Heart of Mary - Memorial - <a href="#" onclick="window.open('http://evangelizo.org/www/popup-saints.php?language=AM&id=9906&fd=0', '', 'width=500,height=450,top=120,left=120,scrollbars=yes')">St. Bernardino Realino, Priest (1530-1616)</a></div><div class="lcright">Jul</div></div></td>
</tr>
<tr><td class="we0"><div id="20110703" class="lcday"><div class="lcleft">Sun 03</div><div class="lcmid"><a href="#" onclick="window.open('http://evangelizo.org/www/popup-saints.php?language=AM&id=9907&fd=0', '', 'width=500,height=450,top=120,left=120,scrollbars=yes')">St. Thomas, Apostle -Feast</a></div><div class="lcright">Jul</div></div></td>
</tr>
</table>
</div>
<script type="text/javascript">
ScrollCalendar();
</script>
ScrollCalendar 函数应该滚动到像 20110701、20110702、20110703 这样的 div ID。
function ScrollCalendar() {
var d = new Date();
calrow = d.formatDate("Ymd");
document.write (calrow);
var offscroll = window.parent.document.getElementById(calrow).offsetTop;
document.write (offscroll);
window.parent.document.getElementById("scrollBoxYe").scrollTop = offscroll;
}
在上面的函数中,使用变量 calrow 检索的 div ID 被正确确定。但是,代码没有提供正确的偏移量 (offscroll) 来滚动 div (scrollBoxYe)。这些是我的问题:
- 如果 calrow 是正确的(确实显示 20110701)。 getelementby ID 会将 calrow 解释为字符串 ID 来检索元素吗?我应该先把 calrow 变成字符串吗?执行此操作的正确函数是什么?
- 如果所有页面信息都在一个文档中,用window.parent.document搜索元素是否错误?代码应该改为 document.getElementbyID("scrollBoxYe").scrollTop 吗?
- 有没有更好的方法使用变量 calrow 滚动scrollBoxYe?
谢谢。
I have an HTML page with this structure.
<div class="scrollBoxYe">
<table class="grid">
<tr><td class="wd0"><div id="20110701" class="lcday"><div class="lcleft">Fri 01</div><div class="lcmid">The Sacred Heart of Jesus, solemnity - <a href="#" onclick="window.open('http://evangelizo.org/www/popup-saints.php?language=AM&id=10309&fd=0', '', 'width=500,height=450,top=120,left=120,scrollbars=yes')">St. Gal, Bishop (c. 489-553)</a>, <a href="#" onclick="window.open('http://evangelizo.org/www/popup-saints.php?language=AM&id=10480&fd=0', '', 'width=500,height=450,top=120,left=120,scrollbars=yes')">Bl. Antonio Rosmini, Priest, Founder of the Institute of Charity (1797-1855)</a></div><div class="lcright">Jul</div></div></td>
</tr>
<tr><td class="we0"><div id="20110702" class="lcday"><div class="lcleft">Sat 02</div><div class="lcmid">Immaculate Heart of Mary - Memorial - <a href="#" onclick="window.open('http://evangelizo.org/www/popup-saints.php?language=AM&id=9906&fd=0', '', 'width=500,height=450,top=120,left=120,scrollbars=yes')">St. Bernardino Realino, Priest (1530-1616)</a></div><div class="lcright">Jul</div></div></td>
</tr>
<tr><td class="we0"><div id="20110703" class="lcday"><div class="lcleft">Sun 03</div><div class="lcmid"><a href="#" onclick="window.open('http://evangelizo.org/www/popup-saints.php?language=AM&id=9907&fd=0', '', 'width=500,height=450,top=120,left=120,scrollbars=yes')">St. Thomas, Apostle -Feast</a></div><div class="lcright">Jul</div></div></td>
</tr>
</table>
</div>
<script type="text/javascript">
ScrollCalendar();
</script>
ScrollCalendar function should scroll to a div ID like 20110701, 20110702, 20110703.
function ScrollCalendar() {
var d = new Date();
calrow = d.formatDate("Ymd");
document.write (calrow);
var offscroll = window.parent.document.getElementById(calrow).offsetTop;
document.write (offscroll);
window.parent.document.getElementById("scrollBoxYe").scrollTop = offscroll;
}
In the function above the div ID which is retrieved with the variable calrow is determined correctly. However, the code doesn't come up with the right offset (offscroll) to scroll the div (scrollBoxYe). These are my questions:
- If calrow is right (does show 20110701). Will getelementby ID interpret calrow as a string ID to retrieve the element? Should I make calrow into a string first? What's the right function to do that?
- Is it wrong to search for the element with window.parent.document if all the page information is in one document? Should the code say instead document.getElementbyID("scrollBoxYe").scrollTop?
- Is there a better way to scroll scrollBoxYe with the variable calrow?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有更好的滚动方式:element.scrollIntoView()
http://msdn.microsoft.com/en-us/library/ms536730(v=vs.85).aspx
它不是标准的一部分,但根据这个所有浏览器都支持。
There is a better way to scroll: element.scrollIntoView()
http://msdn.microsoft.com/en-us/library/ms536730(v=vs.85).aspx
It's not part of the standard but according to this it is supported by all browsers.