У меня есть серия списков, которые я пытаюсь показать / скрыть с помощью 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: объект не поддерживает это свойство или метод», и отображение / скрытие перестает работать.
Также пробовал переключать классы по клику с помощью:
$('#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>
UPDATE:
Пробовал с помощью:
$(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: объект не поддерживает это свойство или метод».