从 Jquery 中的 Div 类名称获取 Id
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15780311/
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
Getting Id from Div Class Name in Jquery
提问by Shivkumar
this is my Html code
这是我的 Html 代码
<div class="span12" id="test" style="width: 810px;">
<ul class="nav nav-tabs">
<li class="active"><a href="/#tab1" data-toggle="tab">New Stream</a></li>
<li><a id="addspan" href="/#C" data-toggle="tab">+</a></li>
</ul>
<div class="tabbable">
<div class="tab-content" id="tabContent">
<div class="tab-pane active" id="tab1">
@Html.Partial("_NewStreamPartial",Model)
</div>
</div>
</div>
</div>
this my javascript
这是我的 javascript
<script type="text/javascript">
$(function () {
var count = 2;
$('#addspan').click(function () {
var Id = $('.tab-pane active').attr('id');
});
});
</script>
I want to get Div Id whoes class name ".tab-pane active"(means i want to get active Div Id) how i can do this?
我想获得 Div Id whoes class name ".tab-pane active"(意味着我想获得活动 Div Id)我该怎么做?
回答by Adil
You can use dot
to join classes
in selector
您可以使用dot
加入classes
选择器
Change
改变
var Id = $('.tab-pane active').attr('id');
To
到
var Id = $('.tab-pane.active').attr('id');
回答by Chamika Sandamal
It should be like this,
应该是这样的
var Id = $('.tab-pane.active').attr('id');
You are using Descendant Selectorhere. but in your dom both classes are in same element. so you have to select by both classes.
回答by ravisolanki07
Try this :
尝试这个 :
$('#addspan').click(function () {
alert($('.tab-pane .active').attr('id'));
});
回答by Ambrose Jesuraj
Try the following code
试试下面的代码
$("#addspan").click(function(){console.log($(this)[0].id);});
回答by itsstephyoutrick
回答by Roger
You can try this
你可以试试这个
$('.tab-pane active').click(function () {
var Id = $(this).attr('id');
});
Hope this helps
希望这可以帮助