twitter-bootstrap 处理引导程序列表-组单击
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34590661/
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
handle bootstrap list-group click
提问by alex111
I'm stuck with html/JS and have no idea how I should handle some events. For example I have a list-group:
我被 html/JS 困住了,不知道我应该如何处理一些事件。例如我有一个列表组:
<div class="col-lg-3 col-md-3 col-sm-3 opciones">
<div class="list-group">
<a href="#" class="list-group-item active">
Tokyo
</a> // barfoobarfoo
<a href="#" class="list-group-item">London</a> // foo
<a href="#" class="list-group-item">Paris</a> // bar
<a href="#" class="list-group-item">Moscow</a> // foobar
<a href="#" class="list-group-item">NY</a> //foobarfoo
</div>
</div>
What I want to do is:
我想做的是:
1)change active elements on click.
1)单击时更改活动元素。
2)call JS function when element is clicked. UPD: So now I know that click events can be handled with JQuery thing.
2)点击元素时调用JS函数。UPD:所以现在我知道可以使用 JQuery 处理单击事件。
What I can't understand is to how to determinate which element is clicked. For example JQuery:
我不明白的是如何确定点击了哪个元素。例如 JQuery:
$('.list-group-item').on('click', function() {
$this = $(this);
$('.active').removeClass('active');
$this.toggleClass('active')
function simpleFunc(someargument) { //value of this argument depends on clicked item and can be (foo|bar|foobar ) etc
document.write(someargument) // here whould be written some text (foo|bar|foobar ) etc
})
There are no tutorials or anything, except this HTML code. Thanks
除了这个 HTML 代码,没有教程或任何东西。谢谢
回答by Hybrid
You can simply use jQuery for this.
为此,您可以简单地使用 jQuery。
For example:
例如:
$('.list-group-item').on('click', function() {
var $this = $(this);
var $alias = $this.data('alias');
$('.active').removeClass('active');
$this.toggleClass('active')
// Pass clicked link element to another function
myfunction($this, $alias)
})
function myfunction($this, $alias) {
console.log($this.text()); // Will log Paris | France | etc...
console.log($alias); // Will output whatever is in data-alias=""
}
Alias would be captures as such:
别名将被捕获如下:
<a data-alias="Some Alias Here">Link<\a>
回答by Chintan Mirani
Here is dynamic jQuery code
这是动态jQuery代码
$(document).ready(function() {
$(".list-group-item").live('click', function(){
$('.active').removeClass('active');
$(this).addClass('active');
console.log($(this).html());
// Code here whatever you want or you can call other function here
});
});
This will help you. Enjoy!
这会帮助你。享受!
回答by Rajesh
You can try something like this:
你可以尝试这样的事情:
function notify(el) {
resetElements();
console.log(el.innerHTML);
el.classList.add('active');
}
function resetElements() {
// Get all elements with "active" class
var els = document.getElementsByClassName("active");
// Loop over Elements to remove active class;
for (var i = 0; i < els.length; i++) {
els[i].classList.remove('active')
}
}
.active {
font-weight: bold;
}
<div class="col-lg-3 col-md-3 col-sm-3 opciones">
<div class="list-group">
<a href="#" onclick="notify(this)" class="list-group-item active">
Tokyo
</a>
<a href="#" onclick="notify(this)" class="list-group-item">London</a>
<a href="#" onclick="notify(this)" class="list-group-item">Paris</a>
<a href="#" onclick="notify(this)" class="list-group-item">Moscow</a>
<a href="#" onclick="notify(this)" class="list-group-item">NY</a>
</div>
</div>

