简体   繁体   English

Webkit Javascript导致页面跳转

[英]Webkit Javascript Causes Page Jumping

Using this code 使用此代码

v=new Date();
var bxx=document.getElementById('bxx');
function t(){
    n=new Date();
    ss={time};
    s=ss-Math.round((n.getTime()-v.getTime())/1000.);
    m=0;h=0;
    if(s<0){
        bxx.innerHTML='<a ';
    }else{
        if(s>59){m=Math.floor(s/60);s=s-m*60;}
        if(m>59){h=Math.floor(m/60);m=m-h*60;}
        if(s<10){s="0"+s}
        if(m<10){m="0"+m}
        bxx.innerHTML=h+':'+m+':'+s+'<br>{name}</a>';
    }
    window.setTimeout("t();",999);
}
t();

Whenever t() is called, the page jumps to the top of the screen. 每当调用t()时,页面就会跳到屏幕顶部。 Any ideas? 有任何想法吗?

The {} are server parsed variables, but they should be causing any problem. {}是服务器解析的变量,但是它们应该引起任何问题。

By jumping, I mean that the page, scrolled to the bottom, suddenly jumps to the top of the page 跳转是指滚动到页面底部的页面突然跳转到页面顶部

I don't know about the jumping, but that code has a couple of issues: 我不知道跳跃的问题,但是该代码有两个问题:

1) Hopefully all of these variables are declared somewhere that you haven't shown? 1)希望所有这些变量都在您未显示的地方声明? (If not, you're falling prey to the Horror of Implicit Globals , which I'd recommend avoiding.) (如果没有,那么您将成为“隐式全球性恐怖”的猎物,我建议您避免使用。)

2) This code: 2)此代码:

bxx.innerHTML='<a ';

...is asking the browser to parse an invalid HTML fragment. ...要求浏览器解析无效的HTML片段。

3) As is this code: 3)像这样的代码:

bxx.innerHTML=h+':'+m+':'+s+'<br>{name}</a>';

...as h and such are numbers, and so you end up with 1:23:45<br>name</a> . ...因为h和此类是数字,所以您最终得到1:23:45<br>name</a>

The innerHTML property cannot contain partial element tags, because it represents the entire content of an element, and elements can't cross over one another like that (the DOM is a tree, not a weave). innerHTML属性不能包含部分元素标签,因为它表示元素的全部内容,并且元素不能像这样相互交叉(DOM是一棵树,而不是编织)。

You'll want to modify the code so that it's always writing a fully-formed a tag to the bxx.innerHTML property, all in one go. 您需要修改代码,以便始终将完整bxx.innerHTML a标记写入bxx.innerHTML属性。

Once you do that, it could still cause some minor jumping, if the former content of the element has different dimensions than the new content. 一旦执行此操作,如果元素的先前内容与新内容具有不同的尺寸,则可能仍会引起一些小的跳跃。 You can mitigate that with CSS (an inline-block element with width and height, etc.). 您可以使用CSS(具有宽度和高度等的内联块元素)缓解这种情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM