他の人が言及したようにあなたが代わりに.addClass
メソッドを使用することができます。アニメーションについてコメントをrepsonseでは、このような何かを行うことができます:
var flashNoticeTimeout;
var fadeOutDelay = 1000;
function onPostAdded() {
var $flashNotice = $("#flash_notice"); // Get flash_notice element
$flashNotice.show().addClass("success"); // Show the flash element and then add the 'success' class.
// Need to use .show because the .fadeOut later on will have hidden this element
clearTimeout(flashNoticeTimeout); // Clear any previous timeouts. If the user button adds a post, then adds another post before the element has faded out, this will clear the last timeout so the element isn't hidden before 'fadeOutDelay`
flashNoticeTimeout = setTimeout(function() { // Trigger this anon function after 'fadeOutDelay' milliseconds (1000 milliseconds = 1 second)
$flashNotice.fadeOut(); // Fade out the element
}, fadeOutDelay);
}
は、デモ用のfiddleを参照してください。
それはあなたが何をしたいかによって異なります。あなたは '成功'クラスだけで滞在したいですか? flash_noticeをフラッシュ/点滅/アニメーションして、最新の投稿に適用されたトグルを知ってもらいたいですか?別のクラスに変更しますか?また、idで要素を参照しているため、タグ名の接頭辞を使用する必要はありません。 ( 'div#flash_notice'ではなく' #flash_notice') – Chris
まず、同じidを持つ2つの要素を持つのは間違いです。それにはクラスを使うべきです。ブラウザが機能しなくなったのを見てきました。 – usoban
@Rfvgyhn私は当初アニメーションをやりたいとは思わなかったが、5秒後にメッセージが消えるようにするためにコードが必要となるのは不思議です。 –