I know that I can run a line of javascript code after the page loads on an iPad with UIWebView (which is what I am using), but I do not know what I could type to go through and remove all of the tags. I also would like to be able to do this to only certain parts of the page e.g. only remove tags within a certain tag.反正刪除全部<a href=> tags with javascript after the page loads on an iPad?
2
A
回答
8
You can get all elements by tag name using document.getElementsByTagName()
。所有鏈接都有標籤名稱a
。您可以通過將它們的display
樣式設置爲none
來直觀地刪除它們。
var elements = document.getElementsByTagName('a');
for (var i = 0; i < elements.length; i++) {
elements[i].style.display = 'none';
}
在一定標籤內除去某一標籤的要素,只是調用getElementsByTagName()
有問題的元件上。假設你要隱藏只有<li>
裏面的所有鏈接:
var listitems = document.getElementsByTagName('li');
for (var i = 0; i < listitems.length; i++) {
var anchors = listitems[i].getElementsByTagName('a');
for (var j = 0; j < anchors.length; j++) {
anchors[j].style.display = 'none';
}
}
的element.parentNode.removeChild(element)
也是一個很好的,但它並沒有一個標準for
循環中很好地工作。您需要循環向後:
var elements = document.getElementsByTagName('a');
for (var i = elements.length; i-- > 0;) {
var element = elements[i];
element.parentNode.removeChild(element);
}
更新按照澄清的功能要求:你這樣想取代與代表的聯繫的原始內容的文本節點的鏈接元素?你可以使用Node.replaceChild()
。這裏有一個開球例如:
var elements = document.getElementsByTagName('a');
for (var i = elements.length; i-- > 0;) {
var element = elements[i];
var text = document.createTextNode(element.firstChild.nodeValue);
element.parentNode.replaceChild(text, element);
}
0
想我會發佈一個remove()函數來補充BalusC:
function remove(el){
if(el.parentNode)el.parentNode.removeChild(el);
}
注:如果元素沒有父母,這意味着它不在DOM樹中。 這也意味着它將在GC(垃圾收集器)下次運行時被刪除(只要沒有對它的引用)。
0
如果您要做大量的dom操作,可能需要包含jQuery來抓取元素。刪除項目會更容易一些。例如。
$(function(){
$('.surrounding_class a').remove();
});
0
如果你想刪除的鏈接,但保留其顯示內容(文字,圖像等),你可以將自己的childNodes
的鏈接之前,然後刪除鏈接元素:
var removeLinks = function(context) {
var undefined;
if(context === undefined) {
context = document;
}
if(!context) {
return false;
}
var links = context.getElementsByTagName('a'), i, link, children, j, parent;
for(i = 0; i < links.length; i++) {
link = links[i];
parent = link.parentNode;
if(!link.href) {
continue;
}
children = link.childNodes;
for(j = 0; j < children.length; j++) {
parent.insertBefore(children[j], link);
}
parent.removeChild(link);
}
return context;
};
// Use:
removeLinks(document.getElementById('container'));
相關問題
- 1. php + selenium,如何獲取全部<a href...> tags from one page
- 2. <a href=''> not working on the other pages
- 3. 用<a href> tags
- 4. 與<a href> tags in Javascript/JQuery
- 5. 內部href的<a tag now refreshes the page
- 6. JS/jQuery:如何在做任何事情之前自動換行<a href> tags around <img />s with the href being dynamic based on the img src?
- 7. 檢索全部<a href=....jpg"> tags in PHP
- 8. 有一個<a href=""> tag change another tags class with jquery
- 9. 替代<a href="#"> when the anchor tag only triggers a jQuery action without redirecting the user?
- 10. 刪除默認<a href> action
- 11. 保存的<a href> in the localstorage
- 12. 如何在<img src="">和<a href=""> tags
- 13. <A Href> has a lot of empty space beneath the text
- 14. 如何刪除HTML,如果它不是真正的醜「<a href=」 tags using perl
- 15. 如何返回一個div在<a href> with javascript?
- 16. 編寫<a href=""> on Django
- 17. href表達式<a href="javascript:;"></a>做什麼?
- 18. 怎樣寫像<a href="#id"> which link to the same page in PHP?
- 19. 提取特定<a href> URLs out of the document
- 20. 與HTML <a href...>
- 21. 使用<a href= > tag
- 22. @ Html.ActionLink到<a href="@Url.Action()>
- 23. <a href="#!"> do?
- 24. javascript解析來自<a href> links
- 25. <a href=...> syntax
- 26. 我怎樣才能從<a href=" and have an array element here then end with</a>開始?
- 27. <A href> adding " to URL
- 28. No fbml tags working on page
- 29. 的JavaScript刪除HTML標籤而不是內容中他們並沒有<a > tags with regex
- 30. 正在使用<a href=" ... " target="window_name"> not a good practice?
+1質量回答 – galambalazs 2010-07-09 23:54:24
我猜測,作者想要保留錨標記的內容(顯示文本,圖像,你有什麼),當刪除鏈接。 – eyelidlessness 2010-07-10 04:40:03
@eye:嗯,這是有道理的。我只是把他們的'href'改成'javascript:void(0)',並添加一個'style',使鏈接看起來像純文本(默認文本顏色,沒有下劃線,默認光標)。 @Matthew:請讓我們知道你*實際*想要什麼。也許我會帶一個代碼示例。 – BalusC 2010-07-10 04:41:36