使用 javascript 动态构建 html 标题标签

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12443923/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 16:12:52  来源:igfitidea点击:

build an html title tag dynamically with javascript

javascripthtmldynamictitle

提问by trobbins26

I have many pages on a site im developing, in which each page has its own title tag. In many cases the titles have constant text, for example:

我在一个正在开发的网站上有很多页面,其中每个页面都有自己的标题标签。在许多情况下,标题具有固定文本,例如:

Home | Section 1 | Page 1
Home | Section 1 | Page 2
Home | Section 1 | Page 3, etc...

首页 | 第 1 节 | 第1页
首页| 第 1 节 | 第2页
首页| 第 1 节 | 第 3 页等...

Inserting these titles makes for harder changes later on, not to mention the hassle of remembering to add them. I'm looking to find a method of dynamically generating the title of each page with javascript, based off the location of the page within the folder structure of the site.

插入这些标题会在以后进行更难的更改,更不用说记住添加它们的麻烦了。我正在寻找一种方法,根据页面在站点文件夹结构中的位置,使用 javascript 动态生成每个页面的标题。

Im currently using a script that does something like this for breadcrumbs, but am not sure how to either reference the same script, or build a similar one for page titles. The breadcrumb script is here: http://trcreative.us/dev/jmsracing/js/breadcrumbs.js

我目前使用的脚本为面包屑做类似这样的事情,但我不确定如何引用相同的脚本,或为页面标题构建类似的脚本。面包屑脚本在这里:http: //trcreative.us/dev/jmsracing/js/breadcrumbs.js

and for the most part doing exactly what I need for titles, minus links per each breadcrumb item. (I don't want it to do that for title's obviously).

并且在大多数情况下,完全符合我对标题的需求,减去每个面包屑项目的链接。(我显然不希望它为标题那样做)。

See breadcrumbs applied here: http://trcreative.us/dev/jmsracing/races/pigman-long-and-olympic/

请参阅此处应用的面包屑:http: //trcreative.us/dev/jmsracing/races/pigman-long-and-olympic/

Any help would be appreciated. Thank you

任何帮助,将不胜感激。谢谢

回答by ruswick

The documentobject has a titleproperty that can be explicitly set like this:

document对象有一个title可以像这样显式设置的属性:

document.title = "some_title_here";

回答by elclanrs

Assuming that your url's are as following home/section/page.htmlyou could use this function at the top of any page and it will set the title based on the url if that's what you mean.

假设您的 url 如下所示,home/section/page.html您可以在任何页面的顶部使用此功能,如果这就是您的意思,它将根据 url 设置标题。

function setTitle(extra) {
  document.title = location.pathname.split('/').map(function(v) {
    return v.replace(/\.(html|php)/, '')
      .replace(/^\w/, function(a) { return a.toUpperCase(); });
  }).join(' | ') + (extra && ' | '+ extra);
}

Using this function will generate a title like this: Home | Section | Page. If you want to add specific stuff for a particular page then pass a string as the extraparameter and it will be added to the title ie.

使用此函数将生成如下标题:Home | Section | Page。如果您想为特定页面添加特定内容,则传递一个字符串作为extra参数,它将被添加到标题中,即。

// url: http://mypage.com/home/about/frogs    
setTitle('Frogs are awesome');
console.log(document.title); //=> Home | About | Frogs | Frogs are awesome

回答by lukek

You can use something like (if using javascript)

您可以使用类似的东西(如果使用 javascript)

var getTitleOfPage = function(){
    // logic here to work out the title of the page.
};

var title = getTitleOfPage();
document.title = title;

Alternatively you may be able to use what ever server side language you are using to set the title also.

或者,您也可以使用您正在使用的任何服务器端语言来设置标题。

回答by Muhammad Ummar

Try this code in JQuery

在 JQuery 中试试这个代码

<script type="text/javascript">
  $(document).ready(function() {
    document.title = 'your page title goes here';
  });
</script>