0

当在我的页面上单击文本时,我想获取具有“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;                   
}); 

警报一无所获。

感谢任何帮助。

4

2 回答 2

1

你需要做的是这个

$(this) // starting from the anchor
   .closest('div.network_ip_content ') // find div that wraps the table content
   .prevAll('.ip_header:first') // get first prev div sibling with class=ip_header
   .find('span.network_ip') // find the span
   .text() // get the text

http://jsfiddle.net/XJvZH/

于 2012-11-14T19:03:42.873 回答
0

您确实应该找到一种将锚点链接到跨度的更好方法 - 即使用 ID 或data-属性。

但是由于您要求搜索最近span的向上,因此您可以:

$(".domain").click(function(){
    var $anchor = $(this);
    $("div#list_lm_domain_urls_dialog").dialog('open');
    var domain = $anchor.text();
    var network_ip;
    $anchor.parents().each(function(){
        var $spans = $(this).prevAll().find('span.network_ip');
        if ($spans.length>0) {
            network_ip = $spans.last().text()
            return false;
        }
    });

    alert(network_ip);
    refresh_lm_domain_links(domain,0,100);

    return false;
}); 

这需要锚的所有父节点并从最近的父节点向上循环。.prevAll()对于每个父母,它使用并查找跨度检查以前的兄弟姐妹。最后,它从找到的跨度中获取最后一个 - 即如果有多个跨度,则为“底部”跨度。

工作演示在这里

于 2012-11-14T18:04:49.000 回答