当我单击列表中的锚点时,如何获取父父级

时间:2015-07-24 03:33:11

标签: javascript jquery html

这是代码的显示方式:

<li>
<div data-trgtid="dom-target-15" class="anchor_div">
    <small class="pull-right">
        <a href="#" class="confirm" id="15" name="read" title="Read me"></a>
    </small>
</div>
<div class="divider"></div>

如何在javascript中获取li具体? 请帮忙。 我点击js是:

<script type="text/javascript">
$(function() {
    $(".confirm").click(function() {
        var id = $(this).attr("id");
        var dataString = 'id='+ id ;
        var parent = $(this).parent();

        var tem_elid = 'dom-target-15';

        var pDoc = parent.parentNode;
        // var tem_elid = 'dom-target-'+id;
        // var dt_domid = document.getElementById(tem_elid).getAttribute("data-trgtid");

         alert(pDoc);



    });
});

1 个答案:

答案 0 :(得分:2)

在您的点击处理程序中,this引用点击的anchor元素,因此您可以使用{{3}获取div所属的父anchor喜欢

$(function() {
  $(".confirm").click(function(e) {
    e.preventDefault();
    var id = this.id;
    var tem_elid = $(this).closest('.anchor_div').data('trgtid');
    snippet.log('target: '+ tem_elid);
  });
});
<!-- Provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<ul>
  <li>
    <div data-trgtid="dom-target-15" class="anchor_div">
      <small class="pull-right">
        <a href="#" class="confirm" id="15" name="read" title="Read me">Read me</a>
      </small>
    </div>
    <div class="divider"></div>
  </li>
</ul>