Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 基于URL更新页面标题_Php_Html - Fatal编程技术网

Php 基于URL更新页面标题

Php 基于URL更新页面标题,php,html,Php,Html,假设example.com的是example 如何自动将example.com/#city1的页面标题更新为example city1和example.com/#city2的页面标题更新为example city2,依此类推 我只有一个index.html文件,我不想将php文件添加到我的html文件中,因此请帮助我编写一个简单明了的代码。您必须至少使用javascript 试试这个: var str='example.com/#city1'; var str1=str.substring(0

假设
example.com的
example

如何自动将
example.com/#city1
的页面标题更新为
example city1
example.com/#city2
的页面标题更新为
example city2
,依此类推


我只有一个
index.html
文件,我不想将
php
文件添加到我的html文件中,因此请帮助我编写一个简单明了的代码。

您必须至少使用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;
//在您的情况下,将p替换为标题标签

更改我

如果您想要一个动态解决方案,它将在每次锚定/哈希更改时更新页面标题:

$(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');
});

您是否已将服务器配置为在.html文件中执行PHP代码?我想你说的是HTML标题吧?换句话说,是“”,而不是页面中显示的标题。请记住,“”不一定对用户可见。请在此添加您的代码Yes KIKO,我的意思是没有php或其他脚本语言,我认为JavaScript是不可能的
document.title=str.substring(0,str.indexOf('.')+''+str.substring((0,str.indexOf('.'))+1))
要更改html标题,请键入此行的语法错误:
让hash=location.hash.replace(/^#/,“”)
@Smith尝试将
更改为
var
语法已修复,但标题仍然没有更改,我这样做对吗:
$(function(){//event$(window).on('hashchange',function(){var hash=location.hash.replace(//^#/,'');document title='example'+hash;})//仅在页面加载时设置(如果没有散列,您可能需要调整)$(window).trigger('hashchange');});例如
@Smith您需要在url
#test1
上添加一个锚。请注意,此方法需要。