[英]Javascript: Replace anchor with title
有沒有辦法使用javascript用標題文本替換錨點中的文本?
<a href="#" title="3pk Cotton Boxer Shorts (13 - 14 Years)">3pk Cotton Boxer Short...</a>
如果要對所有鏈接執行此操作,請嘗試以下操作:
var link = document.getElementsByTagName('a');
for(var i =0;i<link.length;i++) {
link[i].innerHTML = link[i].title;
}
為什么,當然有。 只需將錨的innerHTML
屬性設置為與title
屬性相等即可。
不過,我很好奇:為什么您一開始就沒有正確的文字?
var links = document.links;
for (var i = 0; i < links.length; i++)
links[i].innerHTML = links[i].attributes['title'].value;
使用textContent
屬性:
var anchor_list = document.getElementsByTagName('a'); // or however you'd like to select the target <a>'s
for(var i = 0; i < anchor_list.length; ++i) {
var a = anchor_list[i];
if(typeof a.textContent != "undefined") {
a.textContent = a.title;
} else {
a.innerText = a.title; // old IE support
}
}
如果標題中帶有尖括號,則innerHTML
可能無法按預期執行,因為它可以解析為HTML。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.