由于toggle已被弃用,我用它来togle div:
$("#syndicates_showmore").on("click", function () {
if (!clicked) {
$('#syndicates').fadeTo('slow', 0.3, function () {
$(this).css(
{
'height': 'auto',
'overflow': 'none'
});
}).fadeTo('slow', 1);
setTimeout(function () {
$("#syndicates_showmore").text("Show less");
}, 500);
clicked = true;
}
else {
$('#syndicates').fadeTo('slow', 0.3, function () {
$(this).css(
{
'height': '290px',
'overflow': 'hidden'
});
}).fadeTo('slow', 1);
setTimeout(function () {
$("#syndicates_showmore").text("Show more");
}, 500);
clicked = false;
}
});
有没有更干净的方法来做到这一点?
最佳答案
.toggle(function, function, ... ) removed
This is the "click an element to run the specified functions" signature of
.toggle()
. It should not be confused with the "change the visibility of an element" of.toggle()
which is not deprecated. The former is being removed to reduce confusion and improve the potential for modularity in the library. The jQuery Migrate plugin can be used to restore the functionality.
换句话说,您仍然可以像这样使用.toggle
:
var clicked = false;
$("#syndicates_showmore").on("click", function () {
clicked = !clicked;
var showText = "Show " + (clicked ? "more" : "less");
$('#syndicates').toggle(function () {
$("#syndicates_showmore").text(showText);
});
});
关于javascript - jQuery 切换替代方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19774843/