簡體   English   中英

jQuery .on('click')同時切換此li元素的CSS +調用函數

[英]jquery .on('click') toggle css of this li element + call function same time

我有很多列表,並且要在單擊 li元素的css(背景)時啟動一個彈出窗口,該彈出窗口正在工作,但無法打開/關閉切換,從而更改了項目的背景顏色點擊。

這是下面的代碼//在問題的底部是它的底部-是否有人可以讓我知道如何實現上述要求,這將是非常好的,並且非常感謝

function displayChannels() {
    $.each(channel_list, function (index, channel) {
        //Make the code for the channel
        var code_channel = "<li><img src='" + channel._icon + "'/></li>";
        //Display it
        $("#channel-list").append(code_channel);
        //Open a list for the channel's program list
        var code_prog_list = "<ul class='channel_row'>";
        //Add each program in it as a li each time
        $.each(channel._programs, function (index, programme) {
            code_prog_list += "<li><div class='text-left'>" + "<h5>" + programme._hour + ":" + programme._minutes + "</h5>" + "<h6>" + programme._title + "</h6>" + "<p>" + programme._cat + " (" + programme._duree + "mn)" + "</p></div>" + "<div class='programme_icon'><img src='" + programme._icon + "'' alt=''/></div>" + "<div class='desc'>" + programme._desc + "</div>" + "<div class='star'>" + programme._star + "</div>";
            code_prog_list += "</li>";
            //$(this).children().width(programme._width);
            //$(this).css( "width", programme._width + 'px');
            //$( "#channel_row li").width(programme_width);
            //$(this).children('.channel_row li').width(programme._width);
        });
        //Close the list
        code_prog_list += "</ul>";
        //Display it
        $("#prog-grid").append(code_prog_list);
        $("#prog-grid ul").last().resizePrograms();
    });
    $("#loader").hide();
    scrollTime();
    $(".channel_row li").on("click", showPopup);
    $("#popup").on("click", function () {
        $("#popup").toggle(500);
    });
}
function showPopup() {
    var x = "<div id='close_button'></div>";
    $("#popup").html(x).show(500);
    $(this).children().css("background", "red");
}

添加一個類,然后根據需要刪除並添加-

function showPopup() {
    var x = "<div id='close_button'></div>";
    $("#popup").html(x).show(500);
    $('li').removeClass('redClass'); // remove those with the class first
    $(this).children().addClass('redClass'); // add the class to the current selected
}

您可以嘗試在LI本身上使用方便的toggleClass函數,而讓CSS弄清楚如何處理背景色?

在此處查看示例: http : //jsfiddle.net/mmJy3/

$(this).toggleClass("onecolor");

.toggleClass()-JQuery API參考

我使用了您的代碼並在此處創建了一個樣本Fiddle
如上所述,您只需要刪除其他背景色並將當前的背景色設置為所需的顏色即可。 我將您的最后一個功能更改為:

function showPopup() {
var x = "<div id='close_button'></div>";
$("#popup").html(x).show(500);
$(".channel_row li").children().css('background-color','');
$(this).children().css('background-color','red');
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM