例如,我想像这样更改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);
}
答案 0 :(得分:0)
使用在更新时保存值的变量交换它们
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);
}
&#13;
<p title="Title text">Content text</p>
&#13;