2012-08-10 89 views
0

如何將活動頁面標題更改爲h1標題或h1內容?jQuery將標題更改爲活動頁面中的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; 
}); 
+0

我想要將整頁標題更改爲活動頁面h1標題 – 2012-08-10 18:56:09

回答

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; 
}); 
+0

之後爲了改變您需要編寫的內容 – 2012-08-10 18:53:00

+0

document.title = $(h1); ? – 2012-08-10 18:53:20

+0

@BlitzerClarck你在問什麼? – iambriansreed 2012-08-10 18:59:41

0

添加ID: < H1 ID = 「mytitle」 標題= 「接觸」 >接觸</H1 >

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

希望這就是你的樣子ng for

+0

我想更改頁面標題花花公子而非h1標題感謝 – 2012-08-10 18:52:13

0

使標題屬性匹配當前頁的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; 
}); 
+0

對不起,但林新我試圖學習不工作呢? – 2012-08-10 18:54:56

0

如果你想改變頁面的title的價值,你可以嘗試:

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

// or document.title = current; 
+0

謝謝,這也適用 – 2012-08-10 19:20:57