如何在 jQuery 选择器中分配 onclick 函数的变量值

How can I assign a variable value of onclick function in jQuery selector

基本上,我想要一个特定的 div 部分在单击锚点后显示(显示)它的内容。如果该部分的元素标题与按钮标题匹配,则该部分必须仅显示内容。

这里是 HTML 代码:

<a href="#accordion" id="my-link" class="btn btn-primary">Open group 2</a>
<br/>
<a href="#accordion" id="my-link2" class="btn btn-primary">Communications</a>
<br/><br/><br/><br/>
<div class="panel-group" id="accordion">
  <div class="panel panel-default" id="panel1">
    <div class="panel-heading">
      <h4 class="panel-title">
        <a data-toggle="collapse" data-parent="#accordion" href="#collapseOne">
          Section with the title of "Communications" in the div element
        </a>
      </h4>
    </div>
    <div id="collapseOne" class="panel-collapse collapse in" title="Communications">
      <div class="panel-body">
        Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.
      </div>
    </div>
  </div>
  <div class="panel panel-default" id="panel2">
    <div class="panel-heading">
      <h4 class="panel-title">
        <a data-toggle="collapse" data-parent="#accordion" href="#collapseTwo">
          Collapsible Group Item #2
        </a>
      </h4>
    </div>
    <div id="collapseTwo" class="panel-collapse collapse">
      <div class="panel-body">
        Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.
      </div>
    </div>
  </div>
  
</div>

通常情况下,此脚本可以正常工作:

$('#my-link2').click(function(e) {
    var currentAnchor = $(this);
    alert(currentAnchor.text());
    $("[title*='Communications']").collapse('show');
    $('#collapseTwo').collapse('hide');        
});

但是当使用下面的代码行时,它似乎不起作用:

$('#my-link2').click(function(e) {
    var currentAnchor = $(this);
    alert(currentAnchor.text());
    $("[title*='" + currentAnchor() + "']").collapse('show'); <-- I think i'm doing something wrong here
    $('#collapseTwo').collapse('hide');        
});

要对其进行测试,请参考此 jsfiddle:http://jsfiddle.net/tomanow/DSGxz/632/ 并使用上述示例更改 HTML 和脚本。

在此先感谢您,感谢我能得到的任何帮助。

如果您检查浏览器的 JS 控制台,您将看到以下错误:

尝试使用元素的内部文本:

$('#my-link2').click(function(e) {
    var currentAnchor = $(this);
    alert(currentAnchor.text());
    $("[title*='" + currentAnchor.text() + "']").collapse('show');
    $('#collapseTwo').collapse('hide');        
});

通过对按钮进行一些小的修改,添加一个 class 和一个数据属性,您可以为整个 class 个按钮做一个更通用的监听器。

类似于:

HTML

<a href="#accordion" id="my-link" data-panel="2" class="btn btn-primary mytoggle-btn">Open group 2</a>

JS

$('.mytoggle-btn').click(function(e) {
    var $panel = $('#panel' + $(this).data('panel')),
        $content = $panel.find('.panel-collapse');
    // collapse other panels
    $('#accordion .panel-collapse').not($content).collapse('hide');
    // show the associated on to this button instance        
    $content.collapse('show');        
});