使用javascript交换标题和标记

时间:2016-12-25 14:48:39

标签: javascript html replace greasemonkey swap

我在第三方网站上有html代码:

<div class="info_texts">
  <a href="down.php?action=details&amp;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>

现在我看到截短的文本(短文本...)没有整个文本(全文,没有......)

我想用title=""与javacript(greasemonkey脚本)交换<nobr></nobr>

在网站上有很多<div class="info_texts">所以应该替换所有标签。

谢谢!

1 个答案:

答案 0 :(得分:1)

以下是一个示例代码:

// 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; }
}