简体   繁体   English

使用纯JavaScript删除指定ID属性之前的所有元素

[英]Remove all elements before a specified ID attribute using pure javascript

I'm using a web service to query a ticketing interface and pull back all support tickets that are open at any point in time, displaying them in a nice neat table within an iframe as part of our web application. 我正在使用Web服务来查询票务界面,并撤回在任何时间点都打开的所有支持票证,并将其显示在iframe中漂亮的整洁表格中,作为我们Web应用程序的一部分。 My application is written in PL/SQL however I use this to construct my HTML pages and the content that is displayed within my iframe. 我的应用程序是用PL / SQL编写的,但是我用它来构造HTML页面和显示在iframe中的内容。 My main issue is that because the tickets are made up of Email content, there is a lot of useless rubbish (mostly tags) and irrelevant text that is included at the top of the Email content which causes my interface to look trashy. 我的主要问题是,由于票证是由电子邮件内容组成的,因此,电子邮件内容顶部包含很多无用的垃圾(大多是标签)和不相关的文本,这使我的界面看上去很乱。

Within my code I am wrapping the main areas of content that I require in a table with an ID of "ticketsTable" and would like to remove any elements or content within the iframe only that occurs before this tickets table. 在我的代码中,我将所需的主要内容区域包装在ID为“ ticketsTable”的表中,并希望删除iframe中仅出现在该票证表之前的任何元素或内容。

Usually I would use jQuery for this task: 通常我会使用jQuery来完成此任务:

$("#earliercontent").nextUntil("#ticketsTable").andSelf().remove();

however, our system is built using ExtJS so we wish to avoid any potential conflict the jQuery library could cause with this environment. 但是,我们的系统是使用ExtJS构建的,因此我们希望避免jQuery库在此环境下可能引起的任何潜在冲突。 I therefore need a way to process and loop through the contents of the iframe, removing all elements and text that occur before the ticketTable id therefore eradicating all of the unnecessary content. 因此,我需要一种方法来处理和循环遍历iframe的内容,删除在ticketTable id之前出现的所有元素和文本,从而消除所有不必要的内容。

I've already tried this: 我已经尝试过了:

var last = null;
var curr = $('#page1');
while (curr.attr('id') != 'ticketsTable') {
    if (last != null) {
        last.remove();
    }
    last = curr;
    curr = curr.next();
}
if (last != null) {
    last.remove();
}

Source: jQuery remove all elements until id='whatever' found 来源: jQuery删除所有元素,直到找到id ='whatever'

however, as the Email content is variable, there is no specific ID tag I can specify for the script to start at. 但是,由于电子邮件内容是可变的,因此我可以为脚本指定没有特定的ID标记。 Therefore, is there an alternative method I can use (without a library, just pure JavaScript) to remove all elements and content before the ticketsTable? 因此,有没有一种我可以使用的替代方法(没有库,只有纯JavaScript)来删除ticketsTable之前的所有元素和内容?

I must stress that the contents of the ticketsTable must remain intact. 我必须强调,ticketTable的内容必须保持不变。 It is only the content that occur before this within the body that I wish to remove. 我要删除的只是体内之前发生的内容。

http://jsfiddle.net/yJrb7/ http://jsfiddle.net/yJrb7/

var parent = document.getElementById("parent");
var stopID = "stop";
var length = parent.childNodes.length, j = 0;
for(var i=0; i < length; i++){
    if(parent.childNodes[j].id != stopID){
        parent.removeChild(parent.childNodes[j]);
    }
    else{
        j++;
    }
}

When you remove a child node, the array is shifted, so if for example you need to remove two first nodes, then you would call removeChild for index 0 two times. 删除子节点时,数组将移动,因此,例如,如果需要删除两个首个节点,则将两次为索引0调用removeChild。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM