我有一块带有超链接的研究网站地址,允许用户直接通过电子邮件发送研究网站。最终,我想要某种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, CA 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, KY 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, NY 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很新,所以欢迎任何反馈。
谢谢! 布雷特
答案 0 :(得分:1)
试试这个。
$('.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);
});
答案 1 :(得分:0)
只需用this
替换选择器,它应该创建一个引用您单击的项目的JQuery对象。
更新:
$('.doctor-email').click(function(e){
var address = $(this).parents(".row").find(".doctor-address").text();
console.log(address);
})
<div class="doctor-address">740 South Lindy Street<br>J401<br>Burbank, CA 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, KY 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, NY 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>
答案 2 :(得分:0)