更新时 Javascript 文本呈现错误(仅限 webkit)

发布于 2024-11-05 18:49:40 字数 1070 浏览 4 评论 0原文

我正在尝试点击时切换文本。单击“暂停”时,将文本更改为“播放”。

由于某种原因,文本正在更新,但无法正确呈现。就好像 DOM 的那部分正在更新但没有刷新。由于某种原因,这只发生在 webkit 浏览器(Safari 5、Chrome 11)中。 Firefox 4 正在以应有的方式呈现它。

以下是有关该问题的视频:http://www.youtube.com/watch?v=tIRKx25NmYo< /a>

我在视频中使用 Cmd+A 选择文本,这似乎刷新了文本并使其正确显示。

这是代码:

<span class="playercontrols" id="playpause" onclick="toggle(this.id);" style="cursor:pointer;">Pause</span>


<script type="text/javascript">

function toggle(sender){

var t = document.getElementById(sender);
var txt = t.innerHTML;

switch(txt) 
    {
    case 'Pause':
        txt = 'Play';
        pause();
        break;
    case 'Play':
        txt = 'Pause';
        play();
        break;
    default:
        txt = 'Pause';
    }           
t.innerHTML = txt;

}

</script>

编辑:我注释掉了页面上引用和编写的所有其他 javascript 片段,问题仍然存在。我不知道出了什么问题,但看起来并不是碰撞或冲突。


解决了。 (不允许标记此答案,因为它太新了。)

编辑:答案现在添加在下面。

感谢所有的评论。希望这篇文章能够帮助其他遇到同样问题的人。

I'm trying to toggle text on click. When 'Pause' is clicked change the text to 'Play'.

For some reason the text is updating but not rendering correctly. It's as if that part of the DOM is being updated but not refreshed. For some reason this is only happening in the webkit browsers (Safari 5, Chrome 11). Firefox 4 is rendering it the way it should.

Here's a video of the problem: http://www.youtube.com/watch?v=tIRKx25NmYo

I'm using Cmd+A in the video to select the text, which appears to refresh the text and get it to display properly.

Here's the code:

<span class="playercontrols" id="playpause" onclick="toggle(this.id);" style="cursor:pointer;">Pause</span>


<script type="text/javascript">

function toggle(sender){

var t = document.getElementById(sender);
var txt = t.innerHTML;

switch(txt) 
    {
    case 'Pause':
        txt = 'Play';
        pause();
        break;
    case 'Play':
        txt = 'Pause';
        play();
        break;
    default:
        txt = 'Pause';
    }           
t.innerHTML = txt;

}

</script>

EDIT: I commented out every other piece of javascript referenced and written on the page and the problem is still there. I have no idea what's wrong but it doesn't appear to be a collision or conflict.


SOLVED IT. (Not allowed to mark this answered because it's too recent.)

Edit: Answer now added below.

Thanks for all the comments. Hopefully this post will help others in the future with the same problem.

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

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

发布评论

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

评论(2

早乙女 2024-11-12 18:49:40

似乎闪烁文本(将可见性设置为隐藏,然后返回其原始值)可以解决问题。

所以这是工作大约。

t.innerHTML = txt;
/* add the following right after changing the text */
var visibility = t.style.visibility;
t.style.visibility = 'hidden';
setTimeout( function(){
    t.style.visibility = visibility;
}, 1);

演示 http://jsfiddle.net/gaby/SgwsZ/4/

It seems that flashing the text (setting visibility to hidden and then back to its original value) fixes the issue..

So here is the work around.

t.innerHTML = txt;
/* add the following right after changing the text */
var visibility = t.style.visibility;
t.style.visibility = 'hidden';
setTimeout( function(){
    t.style.visibility = visibility;
}, 1);

demo http://jsfiddle.net/gaby/SgwsZ/4/

旧梦荧光笔 2024-11-12 18:49:40

问题出在“playercontrols”类的样式上。

.playercontrols {
    position: relative;
    top: 80px;
    left: 50px;
}

我将它设置为相对位置,这显然使 webkit 的更新渲染变得不正常。

The problem was with the styling on the "playercontrols" class.

.playercontrols {
    position: relative;
    top: 80px;
    left: 50px;
}

I had it set to position relative which apparently sent webkit's update rendering out of whack.

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