0

当您到达当月的页面时,源代码中的 javascript 会突出显示代表今天的行。如何让页面自动向下滚动到该行?页面是http://www.itsmyturnnow.com/HWC/BRP/08.htm,我得到的脚本是源代码。谢谢。

var INTENDED_MONTH = 7 //August
// INTENDED_MONTH is zero-relative
now = new Date().getDate(),
rows = document.getElementById('scripture').rows;
if (new Date().getMonth() != INTENDED_MONTH) {
    // need a value here less than 1, or the box for the first of the month will be in Gold
    now = 0.5
};
for (var i = 0, rl = rows.length; i < rl; i++) {
    var cells = rows[i].childNodes;
    for (j = 0, cl = cells.length; j < cl; j++) {
        if (cells[j].nodeName == 'TD'
  && cells[j].firstChild.nodeValue != ''
  && cells[j].firstChild.nodeValue == now) {
            rows[i].style.backgroundColor = 'red' // 'ffff99' // '#ffd700' // TODAY - gold
        }
    }
}
4

2 回答 2

1

在突出当前诗篇的函数中使用 JQuery:

var INTENDED_MONTH = 7 //August
// INTENDED_MONTH is zero-relative
now = new Date().getDate(),
rows = document.getElementById('scripture').rows;
if (new Date().getMonth() != INTENDED_MONTH) {
    // need a value here less than 1, or the box for the first of the month will be in Gold
    now = 0.5
};
for (var i = 0, rl = rows.length; i < rl; i++) {
    var cells = rows[i].childNodes;
    for (j = 0, cl = cells.length; j < cl; j++) {
        if (cells[j].nodeName == 'TD'
  && cells[j].firstChild.nodeValue != ''
  && cells[j].firstChild.nodeValue == now) {
            rows[i].style.backgroundColor = 'red' // 'ffff99' // '#ffd700' // TODAY - gold
            $('html,body').delay(1000).animate({scrollTop:rows[i].offsetTop}, 500);

        }

    }
}

JSFiddle: http: //jsfiddle.net/QT3v5/22/ ​</p>

于 2012-08-23T17:21:28.953 回答
0

以下代码不需要 jQuery。它将 ID 应用于当前行,以像素为单位计算行的位置,然后滚动到该位置。

var INTENDED_MONTH = 7 //August
// INTENDED_MONTH is zero-relative
now = new Date().getDate(),
rows = document.getElementById('scripture').rows;
if (new Date().getMonth() != INTENDED_MONTH) {
    // need a value here less than 1, or the box for the first of the month will be in Gold
    now = 0.5
};
for (var i = 0, rl = rows.length; i < rl; i++) {
    var cells = rows[i].childNodes;
    for (j = 0, cl = cells.length; j < cl; j++) {
        if (cells[j].nodeName == 'TD'
  && cells[j].firstChild.nodeValue != ''
  && cells[j].firstChild.nodeValue == now) {
            rows[i].style.backgroundColor = 'red' // 'ffff99' // '#ffd700' // TODAY - gold
            rows[i].setAttribute('id', 'currentRow');
            function findPos(obj) {
                var curtop = 0;
                if (obj.offsetParent) {
                    do {
                        curtop += obj.offsetTop;
                    } while (obj = obj.offsetParent);
                return [curtop];
                }
            }
            window.scroll(0,findPos(document.getElementById('currentRow')));
        }
    }
}

它目前正在为我工​​作。

演示

于 2012-08-24T02:26:16.820 回答