【发布时间】:2012-11-03 04:36:37
【问题描述】:
当在我的页面上单击文本时,我想获取具有“network_ip”类的最近(向上)跨度元素的值。这是我的代码:
<div class="ip_header">
<div style="margin-left:30px;">
<div class="flag_and_ip">
<img title="United Kingdom" src="/gearbox/component/ui/htdocs_zend/public/img/mini-flags/gb.gif">
<span class="network_ip">213.171.218.xxx</span>
</div>
<div class="align_count">48</div>
IPs,
<div class="align_count">63</div>
Domains
</div>
</div>
<div class="network_ip_content ui-accordion-content ui-helper-reset ui-widget-content ui-corner-bottom ui-accordion-content-active" style="height: 4417.6px; display: block;" role="tabpanel">
<h4>
213.171.218.97 (
<b>1</b>
Domains)
</h4>
<p>
<a href="http://private.dnsstuff.com/tools/whois.ch?ip=213.171.218.97&src=ShowIP" target="_blank">IP Whois</a>
,
<a href="http://search.live.com/results.aspx?q=ip%3A213.171.218.97" target="_blank">IP Neighbours</a>
</p>
<table class="table table-striped table-bordered table-condensed">
<colgroup>
<tbody>
<tr>
<td>
<a class="domain" href="#">studentjetpacks.com</a>
</td>
<td>
</tr>
</tbody>
</table>
到目前为止我在 jQuery 中的尝试:
$(".domain").click(function(){
$("div#list_lm_domain_urls_dialog").dialog('open');
var domain = $(this).text();
var network_ip = $(this).closest('span.network_ip').text();
alert(network_ip);
refresh_lm_domain_links(domain,0,100);
return false;
});
警报什么都没有。
感谢任何帮助。
【问题讨论】:
-
你需要遍历你的 dom 才能找到元素.. 因为谁知道你的 span 里面是什么 div.. 你没有显示你的整个结构,因为我看到了 2 个额外的关闭 div
-
否决票 您遇到的问题是 network_ip 不是域的祖先。您必须找到一个共同的祖先,因为您的 HTML 示例在这里不完整,所以很难分辨那是什么。
-
@wirey 抱歉,我现在已经修改了。我感兴趣的 span 元素位于 div.flag_and_ip 中,该 div 位于 div 中,内联样式位于 div.ip_header
-
@Sid 我想你错过了你桌子周围的最后一个 div。我在我的回答中添加了它。我解释了我是如何遍历你的结构的
标签: jquery html-parsing dom-traversal dom jquery-traversing