【问题标题】:I have a dropdown in Javascript, but it is working fine in chrome but not in Firefox我在 Javascript 中有一个下拉菜单,但它在 chrome 中运行良好,但在 Firefox 中却没有
【发布时间】:2017-02-07 11:12:03
【问题描述】:

div class="list-show" data-action="option-tray">--> 当点击下拉框时,它应该将显示添加为块,但它总是在 Firefox 中添加隐藏。但它在 chrome 中运行良好。我认为“ $(window).click(function(e) { base.$optionTray.hide(); });" 代码。我无法解决它。

//这是编译成javascript的Typescript文件

private rootSelectionString: string = ".js-multiselectDropdown";
private actionButtonSelectionString: string = "*[data-action='colex']";
private optionTraySelectionString: string = "*[data-action='option-tray']";
private checkboxTraySelectionString: string = "*[data-action='checkbox-tray']";
private selectedClass= "selected";
private $rootElement: JQuery;
private $optionTray: JQuery;
private $checkboxTray: JQuery;
private $actionButton: JQuery;
private dataId;
private listLength;
private searchCheckedBoxes;
private checkBoxes;

constructor(el) {
    var base = this;
    base.$rootElement = $(el);
    base.$optionTray = base.$rootElement.find(base.optionTraySelectionString).eq(0);
    base.$checkboxTray = base.$rootElement.find(base.checkboxTraySelectionString).eq(0);
    base.$actionButton = base.$rootElement.find(base.actionButtonSelectionString).eq(0);
    base.optionPopulation();
    base.attachEvent();
    base.initialValueCapture();
}

private attachEvent() {
    var base = this;
    if(base.$rootElement.length!==0) {
        base.$optionTray.hide();  
        base.$actionButton.click(function(){
            event.stopPropagation();
            base.$optionTray.toggle();
        });

        $(window).click(function(e) {
           base.$optionTray.hide();
        });

        $(".list-show").click(function(event) {
            event.stopPropagation();
        });

        base.$optionTray.find('li[data-id]').click(function() {
            base.$actionButton.find('span').html(""); 
            $(this).toggleClass(base.selectedClass);
            if($(this).hasClass(base.selectedClass)){
                base.$checkboxTray.find('[data-id="'+$(this).data('id')+'"]').attr('checked','1');                  
            }
            else {
                base.$checkboxTray.find('[data-id="'+$(this).data('id')+'"]').removeAttr('checked');
            }    

            base.searchCheckedBoxes = base.$rootElement.find('input:checked').map(function() {
            return $(this).val();
            });
            var dataArr=[];
            $.each(base.searchCheckedBoxes.get(),function(i,v) {
                dataArr.push(v);
            });
            if(dataArr.length!==0) {
                base.$actionButton.find('span').append(dataArr+","); 
            }
            else {
                base.$actionButton.find('span').html("Choose Tag"); 
            }         
       });
    }
}  

//这里是显示下拉菜单的HTML代码

<div class="js-multiselectDropdown" style="float:left">
    <div class="action-btn " data-action="colex"><span>Choose Tag</span></div>
    <div  class="list-show" data-action="option-tray">
        <ul></ul>
    </div>

    <div data-action="checkbox-tray" class="hidden">
         <input type="checkbox" value="O"/>O</br>
         <input type="checkbox" value="G" checked/>G</br>
         <input type="checkbox" value="E"/>E</br>
         <input type="checkbox" value="N" checked/>N</br>
    </div>       
</div>

【问题讨论】:

  • 您检查控制台是否有错误?
  • ReferenceError: event is not defined 当我点击下拉框时出现此错误

标签: javascript jquery html google-chrome


【解决方案1】:

看来,下面的函数没有通过事件属性

base.$actionButton.click(function(){
    event.stopPropagation();
    base.$optionTray.toggle();
});

似乎没有在 function() 上接收事件,我没有 TypeScript 编译器设置,但如果你只是将 event 添加到 function() 定义中,你应该很高兴。

【讨论】:

  • base.$actionButton.click(function(event){ event.stopPropagation(); base.$optionTray.toggle(); });像这样?
  • 欢迎您,不要忘记将答案标记为已接受。
猜你喜欢
  • 1970-01-01
  • 2014-12-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-29
  • 1970-01-01
  • 2019-12-22
相关资源
最近更新 更多