html 页面标题标签中的 jQuery 滚动选取框

发布于 2024-09-30 15:06:42 字数 80 浏览 4 评论 0原文

我想使用 jquery 在我的 html 标题标签中放置一个滚动字幕,但不知道如何操作,并且似乎无法在任何地方在线找到很好的解释。有人可以帮我吗?

I want to place a scrolling marquee in my html title tag using jquery but don't know how and can't seem to find a good explanation online anywhere. Can someone help me please?

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

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

发布评论

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

评论(4

眉黛浅 2024-10-07 15:06:42

如果您只想让它像 marquee 标签一样滚动,那么这并不是很难做到的:

(function titleMarquee() {
    document.title = document.title.substring(1)+document.title.substring(0,1);
    setTimeout(titleMarquee, 200);
})();

这是非常基本的,但应该让您了解如何根据自己的喜好调整它。

That's not very hard to do if you just want it to scroll like the marquee tag:

(function titleMarquee() {
    document.title = document.title.substring(1)+document.title.substring(0,1);
    setTimeout(titleMarquee, 200);
})();

That's pretty basic but should give you an idea on how to tweak it to your liking.

您的好友蓝忘机已上羡 2024-10-07 15:06:42

在 Tatu Ulmanen 的回答中,空格字符存在问题。正如 psarid 评论所述,第一次滚动后,所有空格都被删除。

这是因为 html 解析器会修剪文本。这意味着它会删除文本末尾和开头的空格。当标题滚动时,html 中的标题对象如下所示:

<title>Scrolling Title With Spaces</title>
<title>crolling Title With SpacesS</title>
<title>rolling Title With SpacesSc</title>
<title>olling Title With SpacesScr</title>
...
<title>Title With SpacesScrolling</title>

正如您在上面看到的,我们丢失了单词 ScrollingSpaces 之间的空格。为了防止这种情况,我们需要将原始 document.title 存储在 javascript 代码中的某个位置,并在其末尾添加一个空格或其他内容。然后,我们可以通过滚动另一个变量中的文本来滚动 document.title 。这是 Tatu Ulmanen 的修改后的代码。

var documentTitle = document.title + " - ";

(function titleMarquee() {
    document.title = documentTitle = documentTitle.substring(1) + documentTitle.substring(0,1);
    setTimeout(titleMarquee, 200);
})();

In Tatu Ulmanen's answer, there is a problem with space characters. As psarid stated as comment, after the first scroll, all of the spaces are removed.

This is because html parser trims texts. That means it removes the spaces at the end and at the beginning of the text. When title is scrolling, the title object in html looks like that:

<title>Scrolling Title With Spaces</title>
<title>crolling Title With SpacesS</title>
<title>rolling Title With SpacesSc</title>
<title>olling Title With SpacesScr</title>
...
<title>Title With SpacesScrolling</title>

As you can see above, we lost the space between words Scrolling and Spaces. To prevent that, we need to store original document.title somewhere in our javascript code and put a space or something else to the end of it. Then, we can scroll document.title by scrolling the text in the other variable. Here is the modified code of Tatu Ulmanen.

var documentTitle = document.title + " - ";

(function titleMarquee() {
    document.title = documentTitle = documentTitle.substring(1) + documentTitle.substring(0,1);
    setTimeout(titleMarquee, 200);
})();
还给你自由 2024-10-07 15:06:42

在页面头部添加以下脚本,然后在 body onload 上调用 scrlsts() 函数

<script type="text/javascript">
var scrl = $('title').text();
function scrlsts() {
     scrl = scrl.substring(1, scrl.length) + scrl.substring(0, 1);
     document.title = scrl;
     setTimeout("scrlsts()", 500);
     }
<script>

Add the below script on your page head and then call the scrlsts() function on body onload

<script type="text/javascript">
var scrl = $('title').text();
function scrlsts() {
     scrl = scrl.substring(1, scrl.length) + scrl.substring(0, 1);
     document.title = scrl;
     setTimeout("scrlsts()", 500);
     }
<script>

旧街凉风 2024-10-07 15:06:42

没有 jQuery:

setInterval(function () {
  $("head title").html($("head title").html().substring(1) + $("head title").html().substring(0,1));
}, 300);

No jQuery:

setInterval(function () {
  $("head title").html($("head title").html().substring(1) + $("head title").html().substring(0,1));
}, 300);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文