【问题标题】:jQuery removeClass seems not to workjQuery removeClass 似乎不起作用
【发布时间】:2016-01-10 02:26:02
【问题描述】:

我正在尝试从“li”中删除类,但它不起作用。这是我的功能:

function setStore($s, id, name) {
    //alert("length: " + $s.parents('ul.popup-cbo-menu').find('li.selected').length);
    $s.parents('ul.popup-cbo-menu').find('li.selected').removeClass('selected');
    //alert("length: " + $s.parents('ul.popup-cbo-menu').find('li.selected').length);

    //$s.parent('li').addClass('selected');
    $('#cboStores').val(name);
}

我可以通过发出警报来确认“已选择”已被删除。但是由于某种原因,它并不影响实际的“li”。

这里是 jsfiddle:http://jsfiddle.net/87xswem7/9/。 但是,由于某种原因,jsFiddle 找不到 setStore() 函数。

这是我创建弹出框的方法:

$(document).ready(function () {
    $("#cboStores").popover({
        viewport: 'body',
        trigger: 'click',
        placement: 'bottom',
        html: true,
        content: '<ul class="popup-cbo-menu">' +
                 '   <li><a href="#" onclick="setStore($(this), -1, \'All Stores\')">All Stores</a></li>' + 
                 '   <li class="selected"><a href="#" onclick="setStore($(this), 1, \'My Foot\')">My Foot</a></li>' +
                 '   <li><a href="#" onclick="setStore($(this), 2, \'My Foot 1\')">My Foot 1</a></li>' +
                 '</ul>',
        template: '<div class="popover cont-popup-cbo-menu" role="tooltip">' +
                  '   <div class="arrow"></div><h3 class="popover-title"></h3>' +
                  '   <div class="popover-content"></div>' +
                  '</div>'
    }).blur(function () {
        $(this).popover('hide');
    });
});

这是我在 html 中的输入框:

<input type="text" id="cboStores" value="My Foot" />

【问题讨论】:

    标签: jquery twitter-bootstrap-3 bootstrap-popover


    【解决方案1】:

    您遇到的主要问题是您尝试使用 setStore 函数来修改弹出框的内容,假设它在 DOM 中正常存在:

    function setStore($s, id, name) {
        //alert("length: " + $s.parents('ul.popup-cbo-menu').find('li.selected').length);
        $s.parents('ul.popup-cbo-menu').find('li.selected').removeClass('selected');
        //alert("length: " + $s.parents('ul.popup-cbo-menu').find('li.selected').length);
    
        //$s.parent('li').addClass('selected');
        $('#cboStores').val(name);
    }
    

    但是,您定义的弹出框的内容并不像您期望的那样存在于 DOM 中。内容实际上是弹出框的一个属性,可以这样记录:

    console.log($("#cboStores").data('bs.popover').options.content)
    

    现在,为了做你想做的事,你必须能够动态修改弹出框的内容。

    这是我引用的一个帖子:Bootstrap popover content cannot changed dynamically

    所以,我所做的是从您的 javascript 中删除 HTML 内容语义,然后将其直接放入 HTML 中以供 javascript 操作:

    <input type="text" id="cboStores" placeholder="Select Store" value="My Foot" style="width:150px;height:30px;" />
    
    <div id="popoverContent" class="hidden">
        <ul class="popup-cbo-menu">
            <li><a href="#" onclick="setStore($(this), -1, 'All Stores')">All Stores</a></li>
            <li class="selected"><a href="#" onclick="setStore($(this), 1, 'My Foot')">My Foot</a></li>
            <li><a href="#" onclick="setStore($(this), 2, 'My Foot 1')">My Foot 1</a></li>
        </ul>
    </div>
    

    请注意,我将所有内容都包裹在一个隐藏的 div 中,该 div 将用于引用弹出框内容。现在,新的 setStore 函数如下所示:

    function setStore($s, id, name) {                                                               
        $('#popoverContent').find('li.selected').removeClass('selected');
        $('#popoverContent').find('a').filter(function(){
            return $(this).text() === $s[0].innerHTML;
        }).parent().addClass('selected');
        $('#cboStores').val(name);
        $("#cboStores").data('bs.popover').options.content = $('#popoverContent').html();
    }
    

    并且将javascript中popover初始化中的popover内容改为:

    $('#popoverContent').html()
    

    这样,您实际上所做的是修改 HTML 的内容,然后设置弹出框的内容以反映 HTML 中的更改。

    小提琴:http://jsfiddle.net/87xswem7/12/

    【讨论】:

    • 注意jsfiddle不行,需要放入$(document).ready(function () { });并将 jsfiddle 设置为“No wrap - in ”。这对我有用jsfiddle.net/87xswem7/13
    • 在我在答案末尾提供的小提琴中,它对我来说效果很好:jsfiddle.net/87xswem7/12。我很高兴能帮上忙! :)
    【解决方案2】:

    我知道为什么:

    对于您的小提琴,您已经将 JS 包装在 onload 中:

    但是,你再做一次:

    $(document).ready(function () {
      //...
    }
    

    没有必要这样做。只需将其更改为不换行即可。

    【讨论】:

    • 我不知道这个选项。 +1 分享:)
    【解决方案3】:

    使用

    setStore = function($s, id, name) {
        //alert("length: " + $s.parents('ul.popup-cbo-menu').find('li.selected').length);
        $s.parents('ul.popup-cbo-menu').find('li.selected').removeClass('selected');
        //alert("length: " + $s.parents('ul.popup-cbo-menu').find('li.selected').length);
    
        //$s.parent('li').addClass('selected');
        $('#cboStores').val(name);
    }
    

    而不是

    function setStore($s, id, name) {
        //alert("length: " + $s.parents('ul.popup-cbo-menu').find('li.selected').length);
        $s.parents('ul.popup-cbo-menu').find('li.selected').removeClass('selected');
        //alert("length: " + $s.parents('ul.popup-cbo-menu').find('li.selected').length);
    
        //$s.parent('li').addClass('selected');
        $('#cboStores').val(name);
    }
    

    它为我解决了(fiddle)

    【讨论】:

    • 你能解释一下定义含义的区别吗?
    • 嗯……它现在正在识别 setStore() 函数,但为什么呢?有什么区别?谢谢...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-25
    • 2021-11-27
    • 2011-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多