Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用jQuery获取span元素的内容?_Jquery_Html Parsing_Dom Traversal_Dom_Jquery Traversing - Fatal编程技术网

如何使用jQuery获取span元素的内容?

如何使用jQuery获取span元素的内容?,jquery,html-parsing,dom-traversal,dom,jquery-traversing,Jquery,Html Parsing,Dom Traversal,Dom,Jquery Traversing,当在我的页面上单击一个文本时,我希望获得最近(向上)的span元素的值,该元素的类为“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-f

当在我的页面上单击一个文本时,我希望获得最近(向上)的span元素的值,该元素的类为“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>
警报没有任何提示


感谢您的帮助。

您真的应该找到一种更好的方法将锚链接到span,即使用id或
数据-
属性

但是,由于您要求搜索最近的向上
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()
检查以前的同级并查找跨度。最后,它从找到的跨度中取最后一个,即如果有多个跨度,则取“底部”跨度


.

您需要做的是

$(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

您需要遍历dom以查找元素。。因为谁知道你的跨度是多少。。你没有展示你的整个结构,因为我看到了两个额外的结束divs down vote。你遇到的问题是网络ip不是域的祖先。您必须找到一个共同的祖先,很难说这是什么,因为您的HTML示例在这里不完整。@wirey抱歉,我现在已经修改了。我感兴趣的span元素位于div.flag_和_ip中,它位于div.ip中具有内联样式的div中_header@Sid我想你错过了你桌边的最后一个点球。。我把它加在我的答案里了。我解释了我是如何穿越你的结构的
$(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