使用 javascript 交换标题和标签
Swap title and tag using javascript
我在第 3 方网站中有 html 代码:
<div class="info_texts">
<a href="down.php?action=details&id=111"
onclick="get_file(111); return false;"
title="TOO LONGER TEXT, TOO LONGER TEXT, TOO LONGER TEXT, TOO LONGER TEXT, TOO LONGER TEXT, TOO LONGER TEXT, TOO LONGER TEXT">
<nobr>TOO LONGER TEXT, TOO LONGER TEXT, TOO...</nobr>
</a>
</div>
现在我看到没有完整文本(全文,没有...)的截断文本(短文本...)
我想用 javacript(greasemonkey 脚本)title=""
和 <nobr></nobr>
交换
网站上有很多 <div class="info_texts">
所以应该替换所有标签。
谢谢!
这是一个示例代码:
// get all a tags with title inside div info_texts
var a = document.querySelectorAll('div.info_texts a[title]');
// loop through them
for (var i = 0, len = a.length; i < len; i++) {
// use one of these
// --------------------------------
// 1- replaces the entire content including <nobr>
a[i].textContent = a[i].title;
// 2- if you want to keep <nobr>
var nobr = a[i].querySelector('nobr');
if (nobr) { nobr.textContent = a[i].title; }
// 3- combination of both above methods
var nobr = a[i].querySelector('nobr');
if (nobr) { nobr.textContent = a[i].title; }
else { a[i].textContent = a[i].title; }
// 4- more error checking, in case title is blank
if (!a[i].title.trim()) { continue; }
var nobr = a[i].querySelector('nobr');
if (nobr) { nobr.textContent = a[i].title; }
else { a[i].textContent = a[i].title; }
}
我在第 3 方网站中有 html 代码:
<div class="info_texts">
<a href="down.php?action=details&id=111"
onclick="get_file(111); return false;"
title="TOO LONGER TEXT, TOO LONGER TEXT, TOO LONGER TEXT, TOO LONGER TEXT, TOO LONGER TEXT, TOO LONGER TEXT, TOO LONGER TEXT">
<nobr>TOO LONGER TEXT, TOO LONGER TEXT, TOO...</nobr>
</a>
</div>
现在我看到没有完整文本(全文,没有...)的截断文本(短文本...)
我想用 javacript(greasemonkey 脚本)title=""
和 <nobr></nobr>
交换
网站上有很多 <div class="info_texts">
所以应该替换所有标签。
谢谢!
这是一个示例代码:
// get all a tags with title inside div info_texts
var a = document.querySelectorAll('div.info_texts a[title]');
// loop through them
for (var i = 0, len = a.length; i < len; i++) {
// use one of these
// --------------------------------
// 1- replaces the entire content including <nobr>
a[i].textContent = a[i].title;
// 2- if you want to keep <nobr>
var nobr = a[i].querySelector('nobr');
if (nobr) { nobr.textContent = a[i].title; }
// 3- combination of both above methods
var nobr = a[i].querySelector('nobr');
if (nobr) { nobr.textContent = a[i].title; }
else { a[i].textContent = a[i].title; }
// 4- more error checking, in case title is blank
if (!a[i].title.trim()) { continue; }
var nobr = a[i].querySelector('nobr');
if (nobr) { nobr.textContent = a[i].title; }
else { a[i].textContent = a[i].title; }
}