2016-05-28 50 views
0

的內容比如我要改變,像這樣的HTML:試圖換一個段落的標題屬性與段落

<p title="Title text">Content text</p> 

成這樣:

<p title="Content text">Title text</p> 

到目前爲止,我此代碼將使title值等於什麼是<p>

window.addEventListener("load",init); 

function init(){ 

var b = document.querySelector("p"); 
b.setAttribute("title",b.textContent); 

console.log(b); 
} 
+1

當然,你需要覆蓋它之前檢索當前標題。因此,使用'getAttribute()'並將其存儲到一個臨時變量中,並在該屬性設置後使用該變量來設置'.textContent'。 –

回答

0

瑞士法郎AP使用它們持有的值的變量,同時更新

window.addEventListener("load", init); 
 

 
function init() { 
 
    var b = document.querySelector("p"); 
 
    var temp = b.textContent; 
 
    b.textContent = b.getAttribute("title"); 
 
    b.setAttribute("title", temp); 
 
    console.log(b); 
 
}
<p title="Title text">Content text</p>