按ID滚动div

发布于 2024-11-19 09:18:05 字数 2780 浏览 3 评论 0原文

我有一个具有这种结构的 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&nbsp;-&nbsp;<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>,&nbsp;&nbsp;<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&nbsp;-&nbsp;<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)。这些是我的问题:

  1. 如果 calrow 是正确的(确实显示 20110701)。 getelementby ID 会将 calrow 解释为字符串 ID 来检索元素吗?我应该先把 calrow 变成字符串吗?执行此操作的正确函数是什么?
  2. 如果所有页面信息都在一个文档中,用window.parent.document搜索元素是否错误?代码应该改为 document.getElementbyID("scrollBoxYe").scrollTop 吗?
  3. 有没有更好的方法使用变量 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:

  1. 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?
  2. 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?
  3. Is there a better way to scroll scrollBoxYe with the variable calrow?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦回梦里 2024-11-26 09:18:05

有更好的滚动方式: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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文