2012-08-10 92 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