Firefox:Dive Into Greasemonkey/4.10. Replacing an element with new content
Mozilla中文Wiki
您的位置:Dive Into Greasemonkey → 公用模式 → 更新元素内容
[编辑]
4.10. 更新元素内容
如果您已经找到了一个元素,您就可以通过 replaceChild 函数来更新它的内容。
例子:更新图片的替代文本(假设页面上存在一个 ID="annoyingsmily" 的元素)
var theImage, altText;
theImage = document.getElementById('annoyingsmily');
if (theImage) {
altText = document.createTextNode(theImage.alt);
theImage.parentNode.replaceChild(altText, theImage);
}
| 如果您想用一大块 HTML 代码来更新元素内容, 您可以先将这些 HTML 代码构造为一个字符串,然后设置元素的 innerHTML 属性。 |
[编辑]
实例
- Frownies (http://diveintogreasemonkey.org/download/frownies.user.js)
← 删除一个元素

![[首页]](/stylesheets/images/wiki.png)