我尝试在页面上检索数据。 我无法改变标记。只有控制台才能使用。
<div class="dataContainer" style="display: block;">
<h4>titre</h4>
<b>Type:</b> Affiliate<br>
<b>Location:</b> 400 Jackson Boulevard, Los Angeles, CA, 90002<br>
<b>Phone:</b> (xxx) yyy-zzzz <br>
<b>Fax:</b> (xxx) yyy-zzzz <br>
<b>Contact Person:</b> John Doe<br>
<b>Email Address:</b> <a href="mailto:john@doe.edu">john@doe.edu</a><br>
<b>Website:</b> <a href="http://example.com" target="_blank">http://example.com</a><br>
<b>Designations:</b> job title
</div>
要检索联系人的姓名,我试过:
var b = jQuery(jQuery('.dataContainer').children('b:contains(Contact)').nextSibling);
alert(b.text());
如何在</b>
之后选择字符串?
答案 0 :(得分:5)
.children()
返回一个jQuery对象,所以要调用nextSibling
你需要获取dom元素引用
var b = jQuery(jQuery('.dataContainer').children('b:contains(Contact)')[0].nextSibling);
alert(b.text());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="dataContainer" style="display: block;">
<h4>titre</h4>
<b>Type:</b> Affiliate
<br>
<b>Location:</b> 400 Jackson Boulevard, Los Angeles, CA, 90002
<br>
<b>Phone:</b> (xxx) yyy-zzzz
<br>
<b>Fax:</b> (xxx) yyy-zzzz
<br>
<b>Contact Person:</b> John Doe
<br>
<b>Email Address:</b> <a href="mailto:john@doe.edu">john@doe.edu</a>
<br>
<b>Website:</b> <a href="http://example.com" target="_blank">http://example.com</a>
<br>
<b>Designations:</b> job title
</div>