1. 程式人生 > >js獲取修改title與jQuery獲取修改title

js獲取修改title與jQuery獲取修改title

js修改title

title在html中屬於特殊的節點元素.因為它可以使用doucment.getElementsByTagName(“title”)[0]來獲取它的標題,但卻無法用doucment.getElementsByTagName(“title”)[0]用更改它的值.

但是,總有解決的方法. 在javascript中,修改title的方法是:

document.title = 'xxxxxx';

<body></body>之間的節點元素用document.getElementsByTagName()來獲取或修改。documentElement代表的是<html></html>

中間的所有東西.

js會使用document.title來代替網頁的title的原因是,title,head,body等標籤是預設的唯一標籤,所以 document.title和document.body可以直接找到結果。 而title是一個結構的標籤,也就是說title內,只能加入nodetext.而不能再加別的元素,於是它的使用又具有特殊性,為了不讓程式碼出錯。於是js中規定document.title這個物件只有一個屬性(也可以說是沒有屬性),document.title本身就是document物件的一個屬性.而不是它的一個子物件.於用document.title=”“來更改title。

jQuery修改title

獲取title的內容

$(document).attr("title","");
$("title").html("");

修改title的內容

$(document).attr("title","修改title的內容");
$("title").html("修改title的內容");