admin管理员组文章数量:1329260
I have a list-group like this:
<div class="list-group list">
<a href="#" class="list-group-item active" id="leftOverlayTitle">
Cras justo odio
</a>
<a href="#" class="list-group-item">Dapibus ac facilisis in</a>
</div>
I wonder how I can check with plain Javascript, which element is highlighted (selected).
Any suggestions?
I have a list-group like this:
<div class="list-group list">
<a href="#" class="list-group-item active" id="leftOverlayTitle">
Cras justo odio
</a>
<a href="#" class="list-group-item">Dapibus ac facilisis in</a>
</div>
I wonder how I can check with plain Javascript, which element is highlighted (selected).
Any suggestions?
Share Improve this question asked Sep 10, 2015 at 7:38 progNewbieprogNewbie 4,8329 gold badges56 silver badges121 bronze badges 2- Do you want to get the element which has an active class? – m4n0 Commented Sep 10, 2015 at 7:53
-
1
Try this -
$("div#list-group a.active").html()
– Vishnu Atrai Commented Sep 10, 2015 at 8:27
3 Answers
Reset to default 2You can use
var x = document.getElementsByClassName("list-group-item");
for (i = 0; i < x.length; i++) {
x[i].onclick=function(){
console.log(this);
}
}
If you want to get highlighted (active) items only:
var items = document.getElementsByClassName("list-group-item active");
To get all items and then do the checking with each separately:
var items = document.getElementsByClassName("list-group-item");
for (var i = 0; i < items.length; i++) {
if ((' ' + items[i].className + ' ').indexOf(' active ') > -1)
items[i].setAttribute('style', 'color: #F00');
else
items[i].setAttribute('style', 'color: #CCC');
}
Either way, the key to solution is getElementsByClassName
method. You can read more about it: https://developer.mozilla/en-US/docs/Web/API/Document/getElementsByClassName
let seleccionado= $(".list-group a.active").html()
let seleccionado = $("#ListaEnlaces a.active").html();
本文标签: javascriptHow to get selected item in listgroup in bootstrapStack Overflow
版权声明:本文标题:javascript - How to get selected item in list-group in bootstrap - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742270161a2444154.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论