我是 javascript 的新手。我有这个示例表。我希望能够获得“http://www.msn.com ”,但未能如愿。我应该怎么做?
提前致谢
j
<body>
<div id="tableContainer">
<table width="100%">
<thead>
<tr>
<th width="16%" > </th >
<th width="62%"> Otras acciones</th >
<th class="sort" width="2%"> Código certificado</th>
<th class="sort" > Descripción</th>
</tr>
</thead>
<tbody>
<tr>
<td class="iconos" >
<span class="sigAccion">
<a href="#" class="sigIcnHref" title="Duplicar" />
<span class=" btnDuplicar">
</span></a>
<a href="http://www.msn.com" class="sigIcnHref" title="Modificar" />
<span class=" btnModificar">
</span></a>
</span> </td>
<td class="AccionRegistro">
<ul>
<li>
<a href="#" >Docència </a></li>
<li>
<a href="#" >Matrícula(S) </a></li>
<li>
<a href="#" >Plans(1) </a></li>
<li>
<a href="#" >Professors(1) </a></li>
<li>
<a href="#" >Horaris(9) </a></li>
<li>
<a href="#" >HorarisProfessors(1) </a></li>
</ul></td>
<td > <sup>2</sup>CAMD</td>
<td> Cert. Alumno Matriculado Ext.</td>
</tr>
</tbody>
</table>
</div>
</body>
最佳答案
直接使用 JavaScript 非常简单。
获取对树上层 a
元素之上已知元素的引用
获取已知元素下的 a
元素列表
将 href
属性与您知道的值相匹配
var anchor = null;
var container;
var items;
container = document.getElementById('tableContainer');
items = container.getElementsByTagName('a');
for (var j = 0; j < items.length; j++) {
if (items[j].href === 'http://www.msn.com') {
anchor = items[j];
break;
}
}
如果您可以直接引用 table
元素然后从那里获取 a
元素列表会更好,但如果那是唯一的 table
在 tableContainer
中没问题。
为了检查 href
属性的已知值,我通常使用不区分大小写的正则表达式,但这应该适合您的情况。
关于javascript:访问表中的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3037554/