div为:target时更改页面标题

时间:2014-09-29 22:10:26

标签: html css title

我正在开发单页导航系统;当div为<title>(url中为#divname)时,是否有办法更改页面的:target

编辑:是的,抱歉,Jquery / javascript解决方案也可以。

2 个答案:

答案 0 :(得分:1)

如果网址包含#somePage,请使用#somePage作为选择器并检索其data-title值。

然后将<title></title>设置为该值。 location.hash生成#somePage

$('a').click(function(event){

    event.preventDefault();

    if(location.hash) {

        var newPageTitle = $(location.hash).data('title');

        $('title').text(newPageTitle);

    }

});

div添加数据属性,并将其值设置为单击该链接时页面标题的值。

<a href="#somePage">Some Page</a>

<div id="somePage" data-title="This Is The Page Title"></div>

答案 1 :(得分:0)

可以通过以下方式完成:

假设你有这个html元素:

<a onclick="onClick1()" href="#test">
    link
</a>

你有这个脚本:

<script>
function onClick1(){
    setTimeout(onClick,100);
}

function onClick(){
    alert(1);
    if(document.URL.indexOf("#test")>=0){
      document.title = "Your title";
    }
}
</script>
然后,您将点击您需要的内容。 这是example