jQuery在activepage中将标题更改为h1标题

时间:2012-08-10 18:32:46

标签: jquery html function title page-title

如何将活动页面标题更改为h1标题或h1内容?

HTML

<section id="contact" class="page_content">
            <h2 title="Contact">Contact</h2>  
</section>

的JavaScript

$("ul.pages li").click(function() {
     $("ul.pages li").removeClass("active");
     $(this).addClass("active"); 
     $(".page_content").hide();
     var activepage = $(this).find("a").attr("href")
     $(activepage).show();                          
     return false;
});

4 个答案:

答案 0 :(得分:2)

尝试;

$("ul.pages li").click(function() {
     $(this).addClass('active').siblings().removeClass('active'); 
     $(".page_content").hide();
     var activepage = $('a', this).attr("href");
     $(activepage).show();
     document.title = $('h1', activepage).first().text();   
     $('title').text( document.title );                        
     return false;
});

答案 1 :(得分:0)

添加ID:      &lt; h1 id =“mytitle”title =“contact”&gt; contact&lt; / h1&gt;

 $("ul.pages li").click(function() {
    $("ul.pages li").removeClass("active");
    $(this).addClass("active"); 
    $(".page_content").hide();
    var activepage = $(this).find("a").attr("href")
    $(activepage).show();

    // NEW:
    $("#mytitle").text("Hello this is changed");
    $("#mytitle").attr("title","we can also change here");     

    return false;
});

希望这就是你要找的东西

答案 2 :(得分:0)

使title属性与当前页面的id匹配,然后

$("ul.pages li").click(function() {
     $("ul.pages li").removeClass("active");
     $(this).addClass("active"); 
     $(".page_content").hide();
     var activepage = $(this).find("a").attr("href")
     $(activepage).show();       
     $("h1[title="+activepage+"]").html("The new Title");
     return false;
});

答案 3 :(得分:0)

如果您想更改页面title的值,可以尝试:

var current = $('h1').attr('title')
$('title').text(current)

// or document.title = current;
相关问题