这是代码的显示方式:
<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);
});
});
答案 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>