尝试在点击链接时应用css到jQuery slideToggle

本文关键字:css 应用 jQuery slideToggle 链接 | 更新日期: 2023-09-27 18:17:32

有一系列的列表,我试图使用jQuery slideToggle显示/隐藏,并需要应用不同的css样式的超链接,做显示/隐藏后,他们被点击,然后切换回原来的风格,一旦他们再次点击。下面的例子是一个资源:http://papermashup.com/jquery-show-hide-plugin/

在下面的代码片段中,有如下内容:

$(toggleDiv).is(":visible") ? toggleClick.text(options.hideText) : toggleClick.text(options.showText);

尝试使用以下命令,它成功了:

$(toggleDiv).is(":visible") ? toggleClick.text(options.hideText).css({ backgroundColor: '#399C05' }) : toggleClick.text(options.showText).css({ backgroundColor: '#FFFFFF' });

但是我需要改变css中的背景图像而不是背景颜色,像这样:

$(toggleDiv).is(":visible") ? toggleClick.text(options.hideText).css({ background: url(/images/icon_image1.gif) }) : toggleClick.text(options.showText).css({ url(/images/icon_image1.gif) });

但是,这会显示" Microsoft JScript runtime error: Object doesn 't support this property or method "并且show/hide停止工作。

也尝试切换类点击:

$('#toggleDiv').toggleClass('show_hideClose', $(this).is(':visible'));

其中show_hidecclose实际上是下面代码片段中show_hide的副本,这导致了上面相同的错误。

所以我有:

$('.show_hide').showHide({
    speed: 1000,  // speed you want the toggle to happen    
    easing: '',  // the animation effect you want. Remove this line if you dont want an effect and if you haven't included jQuery UI
    changeText: 1, // if you dont want the button text to change, set this to 0
    showText: 'View Available Programs', // the button text to show when a div is closed
    hideText: 'Hide Program Listing' // the button text to show when a div is open
});

(function ($) {
    $.fn.showHide = function (options) {
        //default vars for the plugin
        var defaults = {
            speed: 1000,
        easing: '',
        changeText: 0,
        showText: 'Show',
        hideText: 'Hide'
        };
        var options = $.extend(defaults, options);
        $(this).click(function () { 
            $('.toggleDiv').slideUp(options.speed, options.easing); 
            // this var stores which button you've clicked
            var toggleClick = $(this);
            // this reads the rel attribute of the button to determine which div id to toggle
            var toggleDiv = $(this).attr('rel');
            // here we toggle show/hide the correct div at the right speed and using which easing effect
            $(toggleDiv).slideToggle(options.speed, options.easing, function() {
                // this only fires once the animation is completed
                if(options.changeText==1) {
                    $(toggleDiv).is(":visible") ? toggleClick.text(options.hideText) : toggleClick.text(options.showText);
                    //$(toggleDiv).is(":visible") ? toggleClick.text(options.hideText).css({ backgroundColor: '#399C05' }) : toggleClick.text(options.showText).css({ backgroundColor: '#FFFFFF' });      //<-This works with background colors but not with background url
                }
            });
            return false;      
        });
    };
})(jQuery);

超链接位于。net c#重复器中,并包含一个重复器:

<asp:Repeater ID="rptMyContentGroups" runat="server" OnItemDataBound="rptMyContentGroups_OnItemDataBound">
    <ItemTemplate>            
    <a href="#" id="blah" class="show_hide" rel='#                                                <%=rptmyContent.ClientID%>_programList_<%# GetDivClass() %>'>View</a>
        <div id="programList" runat="server" style="display: none;">
        <asp:Repeater ID="rptMyContent" runat="server" OnItemDataBound="rptMyContent_OnItemDataBound">
            <HeaderTemplate>    
                <ul>
            </HeaderTemplate>
            <ItemTemplate>
                <li>
                    <asp:HyperLink ID="hypContentDetail" runat="server" />
                </li>
            </ItemTemplate>
            <FooterTemplate>
                </ul>    
            </FooterTemplate>
        </asp:Repeater>
        </div>
    </ItemTemplate>
</asp:Repeater>
更新:

尝试使用:

$(toggleDiv).is(":visible") ? toggleClick.text(options.hideText).css('background-image', 'url("' + /images/icon_a.gif + '")') : toggleClick.text(options.showText).css('background-image', 'url("' + /images/icon_b.gif + '")');

IE说我错过了a),而我没有。Visual Studio告诉我:"Microsoft JScript运行时错误:对象不支持此属性或方法"。

尝试在点击链接时应用css到jQuery slideToggle

这通常是改变background-image css属性的正确语法:

$(toggleDiv).is(":visible")
    ? toggleClick
        .text(options.hideText)
        .css('background-image', 'url(/images/icon_image1.gif)')
    : ...


但我强烈建议你添加一个选项到你的插件和使用addClass/removeClass。这将使你的插件更可定制,而无需在代码中硬编码图像的url。

// pass the option
$('.show_hide').showHide({
    ...
    showText: 'View Available Programs',
    hideText: 'Hide Program Listing',
    hideClass: 'myClassForHide'
});
// use it in your toggle
$(toggleDiv).is(":visible")
    ? toggleClick
        .text(options.hideText)
        .addClass(options.hideClass)
    : toggleClick
        .text(options.hideText)
        .removeClass(options.hideClass)

Ok…看起来语法和引号起了很大的作用。这似乎可以工作:

$(toggleDiv).is(":visible") ? toggleClick.text(options.hideText).css('background-image', 'url(/images/image1.gif)') : toggleClick.text(options.showText).css('background-image', 'url(/images/image2.gif)');

后悔自己没有早点尝试。