首页 > 解决方案 > 如何在 jQuery 中链接 .html() 和 .fadeIn()?

问题描述

这是我的代码:

$('.container-modal-cash').html().fadeIn(1500);

它抛出:

未捕获的类型错误:$(...).html(...).fadeIn 不是函数

为什么?我该如何解决?

通常我在.html元素内设置内容(使用),然后显示它(使用.fadeIn)。怎么了?

标签: javascriptjquery

解决方案


.html()用于设置元素的内容,或获取元素的内容。FadeIn/Out方法适用于 Jquery 选择器元素(我的意思是$('.test'))。

感谢Trincot,您也可以使用这些fadeIn/fadeOut()方法,当您使用 设置元素的内容时html()

$('.test').html("final content").fadeOut(1500).fadeIn(1500);

请参考下面的例子来证明这一点。

console.log("getting the contents inside");
console.log($('.test').html());
console.log("setting the contents inside");
$('.test').html('changed content');

//fade works on the JQuery selector element.

$('.test').fadeOut(1500);
$('.test').fadeIn(1500);

// you can chain the fadeIn/fadeOut methods like so

$('.test').fadeOut(1500).fadeIn(1500);

// you can also chain the fadeIn/fadeOut when setting the html content like so.

$('.test').html("final content").fadeOut(1500).fadeIn(1500);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="test">content</div>


推荐阅读