gpt4 book ai didi

javascript - 当所有 div 具有相同的类时,如何检索单个文本值

转载 作者:行者123 更新时间:2023-12-03 03:47:13 25 4
gpt4 key购买 nike

我有一组带有超链接的研究网站地址,允许用户直接向研究网站发送电子邮件。最终,我想要某种 jQuery 函数,它返回用户单击的任何电子邮件链接上方的街道地址。

这是我正在使用的函数,其 HTML 如下所示。每次我单击“通过电子邮件发送此网站”时,它都会返回页面上显示的所有地址。我希望它只是显示我选择发送电子邮件的网站的地址。

$('.doctor-email').click(function(e){
var address = $('.doctor-address').text();
console.log(address);
})
<div class="doctor-address">740 South Lindy Street<br>J401<br>Burbank,&nbsp;CA&nbsp;40536</div>
<div class="doctor-directions">
<a href="javascript: void(0);" onclick="javascript: ViewMap('https://maps.google.com/?daddr=740+South+Limestone+Street%2c+Lexington%2c+KY+40536');">
Get Directions
</a>
</div>
</div>
<div class="col-sm-6">
<p class="doctor-email">
<a href="javascript: ContactSite('5859')">
Email this site
</a>
</p>
</div>
</div>
</div>
<div class="col-xs-10 col-sm-9">
<div class="row">
<div class="col-sm-6">
<div class="doctor-address">800 South West Street<br>J401<br>Lexington,&nbsp;KY&nbsp;40536</div>
<div class="doctor-directions">
<a href="javascript: void(0);" onclick="javascript: ViewMap('https://maps.google.com/?daddr=800+South+West+Street%2c+Chicago%2c+IL+40536');">
Get Directions
</a>
</div>
</div>
<div class="col-sm-6">
<p class="doctor-email">
<a href="javascript: ContactSite('5859')">
Email this site
</a>
</p>
</div>
</div>
</div>
</div>
<div class="col-xs-10 col-sm-9">
<div class="row">
<div class="col-sm-6">
<div class="doctor-address">900 Delavan Street<br>J401<br>Buffalo,&nbsp;NY&nbsp;40536</div>
<div class="doctor-directions">
<a href="javascript: void(0);" onclick="javascript: ViewMap('https://maps.google.com/?daddr=900+Delavan+Street%2c+Chicago%2c+IL+40536');">
Get Directions
</a>
</div>
</div>
<div class="col-sm-6">
<p class="doctor-email">
<a href="javascript: ContactSite('5859')">
Email this site
</a>
</p>
</div>
</div>
</div>
</div>

<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>

我对 javascript 非常陌生,所以欢迎任何和所有反馈。

谢谢!布雷特

最佳答案

试试这个。

$('.doctor-email').click(function(e){
//go up to the contextual row, and then find the nested address
var address = $(this).closest('.row').find('.doctor-address').text();
console.log(address);
});

关于javascript - 当所有 div 具有相同的类时,如何检索单个文本值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45336278/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com