单击链接时尝试将 css 应用于 jQuery SlideToggle
有一系列我尝试使用 jQuery SlideToggle 显示/隐藏的列表,并且需要对单击后显示/隐藏的超链接应用不同的 css 样式,然后切换回原始样式一旦再次点击它们。将此示例视为资源: http://papermashup.com/jquery-show-hide- 在下面的代码
片段中,有以下内容:
$(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 本质上是下面代码片段中 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 运行时错误:对象不支持此属性或方法”。
Have a series of lists that I'm trying to show/hide using jQuery slideToggle, and need to apply a different css style to the hyperlinks that do the show/hide after they've been clicked, and then switch back to the original style once they're clicked again. Looked at this example as a resource: http://papermashup.com/jquery-show-hide-plugin/
In the snippets below, there is the following:
$(toggleDiv).is(":visible") ? toggleClick.text(options.hideText) : toggleClick.text(options.showText);
Tried using the following and it worked:
$(toggleDiv).is(":visible") ? toggleClick.text(options.hideText).css({ backgroundColor: '#399C05' }) : toggleClick.text(options.showText).css({ backgroundColor: '#FFFFFF' });
But I need to change the background image in the css instead of the background color like this:
$(toggleDiv).is(":visible") ? toggleClick.text(options.hideText).css({ background: url(/images/icon_image1.gif) }) : toggleClick.text(options.showText).css({ url(/images/icon_image1.gif) });
However, this shows “Microsoft JScript runtime error: Object doesn’t support this property or method” and show/hide stops working.
Also tried switching the classes on click with:
$('#toggleDiv').toggleClass('show_hideClose', $(this).is(':visible'));
Where show_hideClose is essentially a duplicate of show_hide in the snippets below, and this resulted in the same error above.
So I have:
$('.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
});
and
(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);
The hyperlink is inside a .NET C# repeater and contains a repeater:
<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>
UPDATE:
Tried using:
$(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 says I'm missing a ) which I'm not. Visual Studio tells me: "Microsoft JScript runtime error: Object doesn't support this property or method".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这通常是更改背景图像 css 属性的正确语法:
但我强烈建议您向插件添加一个选项并使用 addClass/removeClass。这将使您的插件更加可定制,而无需在代码中硬编码图像的 url。
This is normally the proper syntax to change the background-image css property:
But I strongly suggest you add an option to your plugin and use addClass/removeClass. This will make your plugin more customizable without hardcoding the url of the image in the code.
好吧...看起来语法和引号在这里造成了所有的不同。这似乎有效:
因为没有早点尝试而自责。
Ok... Looks like syntax and quotes made all the difference here. This seems to work:
Kicking myself for not trying that earlier.