這裏是一個可能的解決方案,使用document.createTreeWalker,從所選元素移除屬性和所有這些都是子元素。
HTML
<div id="removeHere" title="titled 0">
<div title="titled 1">some text 1</div>
<div title="titled 2">some text 2
<div title="titled 3">some text 3
<div title="titled 4">some text 4</div>
</div>
</div>
<div title="titled 5">some text 5</div>
</div>
<div title="titled 6">some text 6</div>
的Javascript
(function (global) {
function removeAttributeWalker(node, attribute) {
var treeWalker = document.createTreeWalker(
node,
NodeFilter.SHOW_ELEMENT, {
acceptNode: function() {
return NodeFilter.FILTER_ACCEPT;
}
},
false);
node.removeAttribute(attribute);
while (treeWalker.nextNode()) {
treeWalker.currentNode.removeAttribute(attribute);
}
}
global.addEventListener("load", function onLoad() {
global.removeEventListener("load", onLoad);
removeAttributeWalker(document.getElementById("removeHere"), "title");
}, false);
}(window));
在jsfiddle
更新:如果你需要支持不具備上述方法舊的瀏覽器,那麼下面可以用來代替。
的Javascript
(function (global) {
function removeAttributeWalker(node, attribute) {
if (node.nodeType === 1) {
node.removeAttribute(attribute);
node = node.firstChild;
while (node) {
removeAttributeWalker(node, attribute);
node = node.nextSibling;
}
}
}
global.addEventListener("load", function onLoad() {
global.removeEventListener("load", onLoad);
removeAttributeWalker(document.getElementById("removeHere"), "title");
}, false);
}(window));
在jsfiddle
有與做這樣的事情有關的潛在可訪問性問題。 – 2013-05-08 21:37:31
'[title]'屬性*與* [alt]'屬性不相同。請不要交替使用它們。 – zzzzBov 2013-05-08 21:39:58
這是iframe中的「軟件」嗎? – 2013-05-08 21:43:26