[英]Updating Page Title Based on URL
假設example.com
的<title>
是example
如何自動將example.com/#city1
的頁面標題更新為example.com/#city1
example city1
,將example.com/#city2
的頁面標題自動更新為example.com/#city1
example city2
等等。
我只有一個index.html
文件,我不想將php
文件添加到我的 html 文件中,所以請幫我寫一個簡單干凈的代碼。
如果您需要動態解決方案,則每次錨點/哈希更改時都會更新頁面標題:
$(function(){
// event
$(window).on('hashchange', function() {
let hash = location.hash.replace( /^#/, '' );
document.title = 'example ' + hash;
});
// just to set on page load (you might need to tweak in case there is no hash)
$(window).trigger('hashchange');
});
您必須至少使用 javascript。
嘗試這個:
var str = 'example.com/#city1'; var str1 = str.substring(0,str.indexOf('.'))+' '+str.substring((0,str.indexOf('#')+1)); document.getElementsByTagName('p')[0].innerHTML = str1; //in your case replace p with title tag
<p>Change Me</p>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.