0

私のページでテキストがクリックされたとき、クラス'network_ip'で最も近い(上に行く)span要素の値を取得したいと思います。これが私のコードです:

<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 に答える