Javascript 使用JS捕获下一个主块元素
所讨论的行是var el2。我希望将脚本滚动到*的h元素和它下面的下一个主块元素(h、p、div、pre等)进行hilite。到目前为止,我做得最好的是:Javascript 使用JS捕获下一个主块元素,javascript,Javascript,所讨论的行是var el2。我希望将脚本滚动到*的h元素和它下面的下一个主块元素(h、p、div、pre等)进行hilite。到目前为止,我做得最好的是: /* ScrollToThenFlash */(function(){var d=document;d.body.appendChild(d.createElement('script')).text="(function(){var d=document;function findPos(obj){var curtop=0;if(obj.o
/* ScrollToThenFlash */(function(){var d=document;d.body.appendChild(d.createElement('script')).text="(function(){var d=document;function findPos(obj){var curtop=0;if(obj.offsetParent){do{curtop+=obj.offsetTop}while(obj=obj.offsetParent);return [curtop]}};d.onclick=function(){var dael=d.activeElement;dael.style.background='#ff9 !important';dael.style.color='#444 !important';if(dael.href.indexOf('#')!=-1 && d.all[dael.href.split('#')[1]]){function flash(rep, delay){for(var i=rep;i>0;i--){setTimeout('el.style.background=\"yellow !important\";el2.style.background=\"yellow !important\";', delay*i*2);setTimeout('el.style.background=elbg;el2.style.background=elbg2;', delay*((i*2)+1))};};el=d.all[d.all[dael.href.split('#')[1]].sourceIndex];
el2=d.all[d.all[dael.href.split('#')[1]].sourceIndex+1];elbg=el.style.background;elbg2=el2.style.background;scroll(0, findPos(el));flash(7,130);return false}else return true;/* alert('leaving this page!'); */};})()";})();
它在此处按预期工作(单击页面顶部的导航链接):
但它只在这里显示了h元素:www.opera.com/docs/userjs/specs/
我尝试了以下方面的变化:
el2=el.nextSibling? d.all[el.nextSibling.sourceIndex] : el.parentNode.nextSibling? d.all[el.parentNode.nextSibling.sourceIndex] : d.all[d.all[dael.href.split('#')[1]].sourceIndex+1];
但是浏览器(IE6Moz4)不会有任何功能。欢迎提供任何与html或js相关的建议。使用jQuery并执行$(el)。next()
请格式化您的代码。否决投票。你还不知道如何使用这个平台!
/* For the record I've found the following solution: */
obj_el=d.all[d.all[dael.href.split('#')[1]].sourceIndex];
try{
function get_nextSibling(n){
var x=n.nextSibling;
while(x.nodeType!=1){
x=x.nextSibling
}
;
return x;
}
;
obj_el2=get_nextSibling(obj_el)
}
catch(e){
obj_el2=d.all[d.all[dael.href.split('#')[1]].sourceIndex+1]
};
/* The finished code follows */
(function(){
var d=document;d.body.appendChild(d.createElement('script')).text="
(function(){
var d=document;
d.onclick=function(){
var dael=d.activeElement;dael.style.background='#ff9 !important';dael.style.color='#444 !important';
if(dael.href.indexOf('#')!=-1 && d.all[dael.href.split('#')[1]]){
function flash(rep, delay){
for(var i=rep;i>0;i--){
setTimeout('obj_el.style.background=\"yellow !important\";obj_el2.style.background=\"yellow !important\";', delay*i*2);setTimeout('obj_el.style.background=obj_elbg;obj_el2.style.background=obj_elbg2;', delay*((i*2)+1))};};
obj_el=d.all[d.all[dael.href.split('#')[1]].sourceIndex];try{
function get_nextSibling(n){
var x=n.nextSibling;while(x.nodeType!=1){x=x.nextSibling};return x;};obj_el2=get_nextSibling(obj_el)}catch(e){
obj_el2=d.all[d.all[dael.href.split('#')[1]].sourceIndex+1]};
obj_elbg=obj_el.style.background;obj_elbg2=obj_el2.style.background;
function findPos(obj){
var curtop=0;if(obj.offsetParent){do{curtop+=obj.offsetTop}while(obj=obj.offsetParent);return [curtop]};};
scroll(0, findPos(obj_el));
flash(7,130);return false}
else return true/* alert('Leaving this page!') */;}
;})()"
;})();