javascript - 如何在纯JS中获取没有ID且多次存在的元素

标签 javascript

以下 HTML 是我必须使用的内容。
我无法更改该 HTML 或其构造方式。
纯 JS 也是一项要求,因此请不要使用 jQuery 解决方案

解决方案应该是一个包含我要查找的表的变量。
所以朝这个方向发展: var tableContent = getElementSomehow()

我所指的表格从第 87 行开始,如下所示:<table border=0 CLASS='notifications'>

另一个可能有用的信息是, 所引用的表格,在整个 HTML 中始终是第四个表格。

HTML:

<html>
<head>
<link rel="shortcut icon" href="/nagios3/images/favicon.ico" type="image/ico">
<title>
Alert Notifications
</title>
<LINK REL='stylesheet' TYPE='text/css' HREF='/nagios3/stylesheets/common.css'>
<LINK REL='stylesheet' TYPE='text/css' HREF='/nagios3/stylesheets/notifications.css'>
</head>
<body CLASS='notifications'>

<!-- Produced by Nagios (http://www.nagios.org).  Copyright (c) 1999-2007 Ethan Galstad. -->
<table border=0 width=100%>
<tr>
<td align=left valign=top width=33%>
<TABLE CLASS='infoBox' BORDER=1 CELLSPACING=0 CELLPADDING=0>
<TR><TD CLASS='infoBox'>
<DIV CLASS='infoBoxTitle'>Contact Notifications</DIV>
Last Updated: Wed Jul 8 12:05:50 CEST 2015<BR>
Nagios&reg; Core&trade; 3.5.1 - <A HREF='http://www.nagios.org' TARGET='_new' CLASS='homepageURL'>www.nagios.org</A><BR>
Logged in as <i>kavan</i><BR>
</TD></TR>
</TABLE>
</td>
<td align=center valign=top width=33%>
<DIV ALIGN=CENTER CLASS='dataTitle'>
All Contacts</DIV>
<BR>
<table border=0 cellspacing=0 cellpadding=0 CLASS='navBox'>
<tr>
<td align=center valign=center CLASS='navBoxItem'>
Latest Archive<br><a href='notifications.cgi?contact=all&type=0&archive=1'><img src='/nagios3/images/left.gif' border=0 alt='Latest Archive' title='Latest Archive'></a></td>
<td width=15></td>
<td align=center CLASS='navBoxDate'>
<DIV CLASS='navBoxTitle'>Log File Navigation</DIV>
Wed Jul 8 00:00:00 CEST 2015<br>to<br>Present..</td>
<td width=15></td>
<td><img src='/nagios3/images/empty.gif' border=0 width=75 height=1></td>
</tr>
</table>
<BR><DIV CLASS='navBoxFile'>File: /var/log/nagios3/nagios.log</DIV>
</td>
<td align=right valign=top width=33%>
<form method='GET' action='notifications.cgi'>
<input type='hidden' name='contact' value='all'>
<input type='hidden' name='archive' value='0'>
<table border=0 CLASS='optBox'>
<tr>
<td align=left colspan=2 CLASS='optBoxItem'>Notification detail level for all contacts:</td></tr>
<tr>
<td align=left colspan=2 CLASS='optBoxItem'><select name='type'>
<option value=0 selected>All notifications
<option value=1 >All service notifications
<option value=2 >All host notifications
<option value=8192 >Service custom
<option value=512 >Service acknowledgements
<option value=4 >Service warning
<option value=8 >Service unknown
<option value=16 >Service critical
<option value=32 >Service recovery
<option value=2048 >Service flapping
<option value=16384 >Host custom
<option value=1024 >Host acknowledgements
<option value=64 >Host down
<option value=128 >Host unreachable
<option value=256 >Host recovery
<option value=4096 >Host flapping
</select></td>
</tr>
<tr>
<td align=left CLASS='optBoxItem'>Older Entries First:</td>
<td></td>
</tr>
<tr>
<td align=left valign=bottom CLASS='optBoxItem'><input type='checkbox' name='oldestfirst' ></td><td align=right CLASS='optBoxItem'><input type='submit' value='Update'></td>
</tr>
<tr><td></td><td align=right valign=bottom>
<a href='/nagios3/contexthelp/F1.html' target='cshw' onClick='javascript:window.open("/nagios3/contexthelp/F1.html","cshw","width=550,height=600,toolbar=0,location=0,status=0,resizable=1,scrollbars=1");return true'><img src='/nagios3/images/contexthelp1.gif' border=0 alt='Display context-sensitive help for this screen' title='Display context-sensitive help for this screen'></a>
</td></tr>
</table>
</form>
</td>
</tr>
</table>
<p>
<div align='center'>
<table border=0 CLASS='notifications'>
<tr>
<th CLASS='notifications'>Host</th>
<th CLASS='notifications'>Service</th>
<th CLASS='notifications'>Type</th>
<th CLASS='notifications'>Time</th>
<th CLASS='notifications'>Contact</th>
<th CLASS='notifications'>Notification Command</th>
<th CLASS='notifications'>Information</th>
</tr>
<tr CLASS='notificationsEven'>
<td CLASS='notificationsEven'><a href='extinfo.cgi?type=1&host=VLADIMIR'>VLADIMIR</a></td>
<td CLASS='notificationsEven'><a href='extinfo.cgi?type=2&host=VLADIMIR&service=VMWare%3A+Status'>VMWare: Status</a></td>
<td CLASS='notificationsCRITICAL'>CRITICAL</td>
<td CLASS='notificationsEven'>2015-07-08 12:02:20</td>
<td CLASS='notificationsEven'><a href='config.cgi?type=contacts#it'>it</a></td>
<td CLASS='notificationsEven'><a href='config.cgi?type=commands#notify-by-email'>notify-by-email</a></td>
<td CLASS='notificationsEven'>CRITICAL: Execution time too long!</td>
</tr>
<tr CLASS='notificationsOdd'>
<td CLASS='notificationsOdd'><a href='extinfo.cgi?type=1&host=HP2520-EDV'>HP2520-EDV</a></td>
<td CLASS='notificationsOdd'>N/A</td>
<td CLASS='notificationsHOSTDOWN'>HOST DOWN</td>
<td CLASS='notificationsOdd'>2015-07-08 11:48:20</td>
<td CLASS='notificationsOdd'><a href='config.cgi?type=contacts#it'>it</a></td>
<td CLASS='notificationsOdd'><a href='config.cgi?type=commands#host-notify-by-email'>host-notify-by-email</a></td>
<td CLASS='notificationsOdd'>CRITICAL - Host Unreachable (192.168.1.206)</td>
</tr>
</table>
</div>
</p>

<!-- Produced by Nagios (http://www.nagios.org).  Copyright (c) 1999-2007 Ethan Galstad. -->
</body>
</html>

最佳答案

Another Information that might be helpful is, the table referred to, is always the forth table in the whole HTML.

如果它是文档中的第 4 个表。你可以试试

var tableContent = document.getElementsByTagName('table')[3];

关于javascript - 如何在纯JS中获取没有ID且多次存在的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31290544/

相关文章:

javascript - 检查是否在 jQuery 中选择了单选按钮时的奇怪行为

javascript - 当窗口顶部到达特定元素时向 DIV 添加一个类,否则将其删除

javascript - 将对象数组的特殊元素推送到另一个对象数组

javascript - Jquery 菜单行为不稳定、定位错误和一般错误

javascript - 如何将 Material UI Popper 定位在浏览器的右下角?

javascript - 将在 Angular 生态系统之外定义的对象包含到工厂中

javascript - 保留 Webpack 中模块的顺序

javascript - 无法删除 Javascript 对象中设置的事件监听器

javascript - 如何在 WinDbg 中引用不同脚本中定义的 JavaScript 函数?

javascript - 为什么此代码不生成数字 0?