简体   繁体   English

字符串和var串联,因为jquery选择器不起作用。 可能的原因是关闭

[英]string and var concatenation as jquery selector isn't working. Possible reason is closure

I'm tweaking bootstrap-tabcollapse.js plugin . 我正在调整bootstrap-tabcollapse.js插件 To the original method, in the js file, I've added bootstrap accordion listener show.bs.collapse to the panel: 对于原始方法,在js文件中,我向面板添加了bootstrap手风琴监听器show.bs.collapse

   TabCollapse.prototype._createAccordionGroup = function(parentId, $heading){
        var tabSelector = $heading.attr('data-target'),
            active = $heading.data('bs.tabcollapse.parentLi').is('.active');

        if (!tabSelector) {
            tabSelector = $heading.attr('href');
            tabSelector = tabSelector && tabSelector.replace(/.*(?=#[^\s]*$)/, ''); //strip for ie7
        }

        var $tabPane = $(tabSelector),
            groupId = $tabPane.attr('id') + '-collapse',
            $panel = $(this.options.accordionTemplate($heading, groupId, parentId, active));
        $panel.find('.panel-heading > .panel-title').append(this._tabHeadingToPanelHeading($heading, groupId, parentId, active));
        $panel.find('.panel-body').append($tabPane.contents().detach())
            .data('bs.tabcollapse.tabpane', $tabPane);
     $("#"+groupId).on('show.bs.collapse', function () {
         console.log("show");
        });
        return $panel;
    };

Lines added: 添加的行:

 $("#"+groupId).on('show.bs.collapse', function () {
             console.log("show");
            });

I smell closure problem, but can't quite put my finger on it. 我闻到关闭问题,但不能完全动弹。 So where is it? 那在哪呢

The only reason why it isn't working might be because, the custom event isnt mapped to those elements or the event triggering is not happening correctly (actual reason not known). 它不起作用的唯一原因可能是,自定义事件未映射到那些元素,或者事件触发未正确发生(实际原因未知)。

But from looking at the code, comments and what you are trying to achieve(to my understanding), is, to do something when you are opening a accordion tab. 但是从查看代码,注释以及您想要达到的目的(据我所知)是,当您打开手风琴选项卡时要做一些事情。 So I think you can achieve that by using the API given, instead of tweaking it. 因此,我认为您可以通过使用给定的API来实现这一目标,而无需进行调整。 A simple code would be like this : 一个简单的代码是这样的:

$(document).on("shown.bs.collapse",".panel-collapse",function(e){
   $(".panel-heading").css({"background-color": ""});
   $(e.currentTarget).closest(".panel").find(".panel-heading").css({"background-color":"red"});
 });

Here is a fiddle : https://jsfiddle.net/t9pmq3sz/1/ 这是一个小提琴: https : //jsfiddle.net/t9pmq3sz/1/

Update: 更新:

I experimented with the tweak and managed to get it working somehow, instead of directly using the $("#"+groupId) , Im using it like this: 我尝试了该调整,并设法以某种方式使其工作,而不是直接使用$("#"+groupId) ,而是这样使用Im:

$(document).on("shown.bs.collapse","#"+groupId,function(e){
  console.log(e);
});

Fiddle : https://jsfiddle.net/t9pmq3sz/3/ 小提琴: https : //jsfiddle.net/t9pmq3sz/3/

Hope it helps 希望能帮助到你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM