在一个简单的 jQuery 函数中切换
Toggling around in a simple jQuery Function
假设我得到了这个小 span
,我无法修改它的结构或提供任何周围的元素。它就是这样。但现在我想在其中显示另一个文本,我希望它们在彼此之间切换。
示例:第一个文本停留 1 秒 -> 淡出并显示另一个文本 -> 重复到无限。
是否可以仅使用 toggle() 函数将其存档?我尝试了一下,但没有任何效果。
$(function() {
$('#test').delay(1000).fadeOut(750, function() {
$(this).text('Some other text!').fadeIn(500);
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
<span id="test">This Text!</span>
你可以这样做。
希望对您有所帮助
setInterval(function() {
$("#test").fadeOut(750, function() {
if ($(this).text() == "This Text!") {
$(this).text("Some other text!").fadeIn(500);
} else {
$(this).text("This Text!").fadeIn(500);
}
});
}, 1500);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
<span id="test">This Text!</span>
假设我得到了这个小 span
,我无法修改它的结构或提供任何周围的元素。它就是这样。但现在我想在其中显示另一个文本,我希望它们在彼此之间切换。
示例:第一个文本停留 1 秒 -> 淡出并显示另一个文本 -> 重复到无限。
是否可以仅使用 toggle() 函数将其存档?我尝试了一下,但没有任何效果。
$(function() {
$('#test').delay(1000).fadeOut(750, function() {
$(this).text('Some other text!').fadeIn(500);
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
<span id="test">This Text!</span>
你可以这样做。
希望对您有所帮助
setInterval(function() {
$("#test").fadeOut(750, function() {
if ($(this).text() == "This Text!") {
$(this).text("Some other text!").fadeIn(500);
} else {
$(this).text("This Text!").fadeIn(500);
}
});
}, 1500);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
<span id="test">This Text!</span>