【发布时间】:2011-12-11 03:12:29
【问题描述】:
有一系列列表,我正在尝试使用 jQuery slideToggle 显示/隐藏,并且需要对在单击后显示/隐藏的超链接应用不同的 css 样式,然后切换回再次单击它们后的原始样式。把这个例子看成资源:http://papermashup.com/jquery-show-hide-plugin/
在下面的sn-ps中,有以下内容:
$(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 运行时错误:对象不支持此属性或方法”并且显示/隐藏停止工作。
还尝试在点击时切换类:
$('#toggleDiv').toggleClass('show_hideClose', $(this).is(':visible'));
其中 show_hideClose 本质上是下面 sn-ps 中 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 说我缺少一个 ) ,但我没有。 Visual Studio 告诉我:“Microsoft JScript 运行时错误:对象不支持此属性或方法”。
【问题讨论】:
标签: c# jquery .net css slidetoggle