添加一个隐藏的课程'设置cookie时我的div

时间:2015-09-15 10:45:26

标签: javascript jquery

我想添加一个类"隐藏"到' following_prompt'当用户单击关闭容器时。当他们点击关闭容器时,会设置一个cookie。

MY JS:

jQuery(document).ready(function(){
    // if the cookie exist, hide the element
    var hide = Cookies.getJSON('hide');

    if (hide && hide.element)
       $(hide.element).hide();

    $('#hideshow').on('click', function(event) {        
         $('.following_prompt').hide()

         Cookies.set('hide', {element: '.following_prompt'}, { expires: 7 });
         return false;
         $('.following_prompt').addClass('hidden')
    });
});

2 个答案:

答案 0 :(得分:1)

你可以在jQuery中“链接”方法,所以我会这样做:

$(document).ready(function() {
    $('.paging li a').click(function(e) {
        e.preventDefault();

        // Cancel previous request if there is one
        if(typeof xhr !== 'undefined') {
            xhr.abort();
        }

        // Do the new request
        var xhr = $.ajax({
            url: $(this).attr('href') + '.small',
            success: function(data) {
                 $('.small li').html(data);
                 doPager();
            }
        });
    });
});

请注意,$('#hideshow').on('click', function(event) { $('.following_prompt').addClass('hidden').hide(); Cookies.set('hide', {element: '.following_prompt'}, { expires: 7 }); }); 定位元素的,因此,如果您有多个,那么所有都会$('.following_prompt')添加的类和所有将被隐藏。如果您需要定位一个元素,请为其添加唯一ID,然后使用hidden来定位它。

没有必要使用$('#elementid')语句,所以我将其删除了。

答案 1 :(得分:0)

我看到你的问题的方式,你想要一个类添加到div。要做到这一点,只需使用$(' #divId')。addClass(' className')