当所有div具有相同的类

时间:2017-07-26 19:55:36

标签: javascript jquery html google-tag-manager

我有一块带有超链接的研究网站地址,允许用户直接通过电子邮件发送研究网站。最终,我想要某种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很新,所以欢迎任何反馈。

谢谢! 布雷特

3 个答案:

答案 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,&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>

答案 2 :(得分:0)

要访问个人地址,您应该使用id。

其他方式是Mike在当前上下文中建议使用jQuery prev()函数来获取地址文本。

参见:prev()