简体   繁体   English

选取框,动态更改内容并重新开始滚动

[英]Marquee, change content dynamically and restart scrolling

I have a marquee on my website: 我的网站上有一个marquee

<marquee>Hello! <span id="text">Welcome to my website.</span></marquee>

I want to change dynamically the content of the span with id="text" from marquee : 我想从marquee动态更改id="text"的跨度内容:

 setTimeout(function() { document.getElementById("text").innerHTML = "This is my website. Happy reading!"; }, 5000); 
 <marquee>Hello! <span id="text">Welcome to my website.</span> </marquee> 

All works well. 一切正常。 The problem is that I want to restart the marquee, from the beginning after changing the text. 问题是我想在更改文本后从头开始重新启动选取框。 Imagine first text is a longer one, when changing with another, it will not be entirely readable at first scroll; 想象一下,第一个文本是一个较长的文本,当与另一个文本进行更改时,第一次滚动时将不会完全可读。 the visitor will see it from middle. 访客将从中间看到它。

So, I want to restart the marquee scroll from beginning. 因此,我想从头开始重新启动选取框滚动。

Maybe not what you want or expect. 也许不是您想要或期望的。 But this should restart the marquee. 但这应重新启动选取框。
I would delete and reinsert the marquee in the DOM with a new element. 我将使用新元素删除选取框并将其重新插入DOM中。

 setTimeout(function() { document.getElementById("marquee").innerHTML = '<marquee>Hello! <span id="text">This is my website. Happy reading!</span></marquee>'; }, 5000); 
 <div id="marquee"> <marquee>Hello! <span id="text">Welcome to my website.</span> </marquee> </div> 

Select your marquee and use .start() to restart it. 选择您的选取框,然后使用.start()重新启动它。

 setTimeout(function() { document.getElementById("text").innerHTML = "This is my website. Happy reading!"; document.getElementsByTagName("marquee")[0].start(); }, 5000); 
 <marquee>Hello! <span id="text">Welcome to my website.</span> </marquee> 

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

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