javascript 如何获取 div ID JQUERY
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23677952/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get div ID JQUERY
提问by Apollo Belvedere
I want to get the ID or class if the link button is clicked
如果单击链接按钮,我想获取 ID 或类
<div class="pane alt" id="a">
<h3>Nick says:</h3>
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Morbi malesuada
<p><a href="#" class="btn-delete">Delete</a> <a href="#" class="btn-unapprove">Unapprove</a> <a href="#" class="btn-approve" style="display:none">Approve</a>
the code that i created to process the clicked button is this which resulting "undefined"
我为处理单击的按钮而创建的代码是导致“未定义”的代码
$('.btn-unapprove').click(function(){
var id = $(this).attr('id');
alert(id);
});
so is it possible to get the div id if the link is clicked ? if so how to to do it?
那么如果点击链接,是否有可能获得 div id?如果是这样怎么办?
回答by Ishan Jain
First of all: You should correct you HTML. Because if your HTML is not loaded correctly in DOM than the Jquery/Javascript also not provide correct result.
首先:你应该更正你的 HTML。因为如果你的 HTML 没有在 DOM 中正确加载,那么 Jquery/Javascript 也不会提供正确的结果。
In your HTML you are missing some closing tags like - "</p>, </div>
"
在您的 HTML 中,您缺少一些结束标记,例如 - " </p>, </div>
"
<div class="pane alt" id="a">
<h3>Nick says:</h3>
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Morbi malesuada</p>
<p>
<a href="#" class="btn-delete">Delete</a> <a href="#" class="btn-unapprove">Unapprove</a>
<a href="#" class="btn-approve" style="display:none">Approve</a>
</p>
</div>
There are several ways to do accomplish this task.
有几种方法可以完成此任务。
You Can use Jquery .closest()
你可以使用 Jquery .closest()
$('.btn-unapprove').click(function(){
alert($(this).closest('div.pane.alt').attr('id'));
});
You Can use Jquery .parents()
你可以使用 Jquery .parents()
$('.btn-unapprove').click(function(){
alert($(this).parents('.pane.alt').attr('id'));
});
You Can use Jquery .parent()
你可以使用 Jquery .parent()
$('.btn-unapprove').click(function(){
alert($(this).parent().parent().attr('id'));
});
回答by Sachin
This will help you to work done. Before doing this please correct your code and improve like following. In your code you didn't close the <p> tags.
I suggest don't use anchor tags there go with **fake-link** using <span>..
Refer : How to make an anchor tag refer to nothing?
<div class="pane alt" id="a">
<h3>Nick says:</h3>
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Morbi malesuada</p>
<p>
<a href="#" class="btn-delete">Delete</a>
<a href="#" class="btn-unapprove">Unapprove</a>
</p>
</div>
jQuery:
jQuery:
jQuery(document).ready(function(){
jQuery('.btn-unapprove').click(function(){
alert( jQuery(this).closest('div').attr('id') );
});
});
回答by Balachandran
回答by Hamid Bahmanabady
var id = $(this).parents('.pane').attr('id');
回答by Sudharsan S
Use .closest()
in jquery
.closest()
在 jquery 中使用
$('.btn-unapprove').click(function(event){
event.preventDefault();
var id = $(this).closest('div.pane').attr('id');
alert(id);
});
回答by santik
You have no ids on the buttons elements. If you set it your code will start working.
您在按钮元素上没有 ID。如果您设置它,您的代码将开始工作。
To get id of container use the code
要获取容器的 id,请使用代码
var id = $(this).closest('.pane').attr('id');
inside the 'click' function.
在“点击”功能内。