处理引导程序列表-组单击

ale*_*111 4 javascript twitter-bootstrap

我被 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>
Run Code Online (Sandbox Code Playgroud)

我想做的是:

1)单击时更改活动元素。

2)点击元素时调用JS函数。UPD:所以现在我知道可以使用 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
})
Run Code Online (Sandbox Code Playgroud)

除了这个 HTML 代码,没有教程或任何东西。谢谢

Hyb*_*rid 6

您可以简单地使用jQuery。

例如:

$('.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=""
}
Run Code Online (Sandbox Code Playgroud)

别名将被捕获如下:

<a data-alias="Some Alias Here">Link<\a>
Run Code Online (Sandbox Code Playgroud)