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

如何在jQuery选择器中分配onclick函数的变量值,jquery,selector,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&

基本上,我想一个特定的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*='" + currentAnchor() + "']").collapse('show'); <-- I think i'm doing something wrong here
    $('#collapseTwo').collapse('hide');        
});
$('my-link2')。单击(函数(e){
var currentAnchor=$(此值);
警报(currentAnchor.text());

$(“[title*=”“+currentAnchor()+”])。折叠('show');如果您签出浏览器的JS控制台,您将看到以下错误:

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

$('my-link2')。单击(函数(e){
var currentAnchor=$(此值);
警报(currentAnchor.text());
$(“[title*=”+currentAnchor.text()+“']”)。折叠('show');
$('#collapseTwo')。collapseTwo('hide');
});

通过对按钮进行一些小的修改,添加一个类和一个数据属性,您可以为整个按钮类创建一个更通用的侦听器

比如:

HTML

$('#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');        
});
<a href="#accordion" id="my-link" data-panel="2" class="btn btn-primary mytoggle-btn">Open group 2</a>
$('.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');        
});