jQuery 为什么 jqueryfadeIn() 不能与 .html() 一起使用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1490563/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why doesn't jquery fadeIn() work with .html()?
提问by Edward Tanguay
When you click you a checkbox I want the message to fade in slowly.
当您单击一个复选框时,我希望消息慢慢淡入。
Why doesn't .fadeIn() work in this example?
为什么 .fadeIn() 在这个例子中不起作用?
HTML:
HTML:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8"/>
<title>Text XHTML Page</title>
<link href="css/main.css" rel="stylesheet" type="text/css" media="all"/>
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript" src="javascript/main.js"></script>
</head>
<body>
<div class="checkboxList">
<div class="title">Languages:</div>
<div class="row"><input class="checkbox" type="checkbox"/><span class="label">Ruby</span></div>
<div class="row"><input type="checkbox"/><span class="label">PHP</span></div>
<div class="row"><input type="checkbox"/><span class="label">C#</span></div>
<div class="row"><input type="checkbox"/><span class="label">Python</span></div>
<div class="row"><input type="checkbox"/><span class="label">JavaScript</span></div>
</div>
<p id="message"></p>
</body>
</html>
javascript:
javascript:
google.load("jquery", "1.3.2");
//run when page is loaded
google.setOnLoadCallback(function() {
$('.checkboxList .row').css('color','red');
$('.checkboxList input').attr('checked', true);
$('.checkboxList input').bind('click', function() {
$('#message').html("You clicked on a checkbox.").fadeIn('slow');
});
});
回答by CMS
No fadeIn is done because the #message element is visible, hide it, add the content and fade it in:
没有淡入是因为 #message 元素是可见的,隐藏它,添加内容并淡入:
$('#message').hide().html("You clicked on a checkbox.").fadeIn('slow');
回答by XMaster
after analize this problem, that I have to solve, this is my code, that works to use fadeout, change html and fadein
在分析这个问题之后,我必须解决,这是我的代码,可以使用淡出,更改 html 和淡入淡出
$("#div_big_picture").fadeOut('slow',function(){
$(this).html("<img src='" + str_to_load + "' height='800px' />")
}).fadeIn("slow");
回答by beggs
No idea why but I've had trouble chaining this before. You can get the effect you want by using this less elegant code:
不知道为什么,但我之前在链接它时遇到了麻烦。你可以通过使用这个不太优雅的代码来获得你想要的效果:
google.load("jquery", "1.3.2");
//run when page is loaded
google.setOnLoadCallback(function() {
$('.checkboxList .row').css('color','red');
$('.checkboxList input').attr('checked', true);
$('.checkboxList input').bind('click', function() {
$('#message').hide(); //just in case
$('#message').html("You clicked on a checkbox.");
$('#message').fadeIn('slow');
});
});