• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

无论都删除所有和amp;lt; a href =和amp;gt;在iPad上加载页面后使用javascript标签吗?

用户头像
it1352
帮助1

问题说明

我知道在页面使用UIWebView(这就是我正在使用的)在iPad上加载页面后,我可以运行一行javascript代码,但是我不知道我可以键入什么内容来删除所有标签.我还希望能够仅对页面的某些部分执行此操作,例如仅删除特定标签内的标签.

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.

正确答案

#1

您可以使用 document.getElementsByTagName() .所有链接的标签名称为a.您可以通过将其display样式设置为none来直观地删除它们.

You can get all elements by tag name using document.getElementsByTagName(). All links have the tag name a. You can visually remove them by setting their display style to none.

var elements = document.getElementsByTagName('a');
for (var i = 0; i < elements.length; i  ) {
    elements[i].style.display = 'none';
}

要删除某个标签内某个标签的元素,只需在所讨论的元素上调用getElementsByTagName()即可.假设您只想隐藏<li>中的所有链接:

To remove elements of a certain tag within a certain tag, just invoke getElementsByTagName() on the element in question. Suppose that you want to hide all links inside a <li> only:

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循环中不能很好地工作.您需要循环向后:

The element.parentNode.removeChild(element) is also a good one, but it doesn't work nicely inside a standard for loop. You need to loop backwards:

var elements = document.getElementsByTagName('a');
for (var i = elements.length; i-- > 0;) {
    var element = elements[i];
    element.parentNode.removeChild(element);
}

根据已阐明的功能要求

更新:您是否想用文本节点表示链接原始内容的替换链接元素?您可以为此使用 Node.replaceChild() .这是一个启动示例:


Update as per the clarified functional requirement: you thus want to replace the link element with a text node representing the link's original content? You can use Node.replaceChild() for this. Here's a kickoff example:

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

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /reply/detail/tanhccjhbk
系列文章
更多 icon
同类精品
更多 icon
继续加载