2010-09-21 86 views
1

我正在使用jquery插件上傳文件,插件檢查文件維度並返回相應的錯誤消息。我顯示的錯誤消息是與此代碼。Javascript新手問題

$('#divmsg6').append("<p class = 'red'>Incorrect file dimension, try again</p>"); 

現在,如果用戶繼續嘗試錯誤將繼續追加,這就是append的意思。而不是追加我想我的錯誤代碼被替換每次它找到一個。什麼是JS代碼?

+0

難道你不能只是將divmsg6的innerHTML更改爲每次有任何錯誤消息嗎? – Warty 2010-09-21 06:30:14

回答

2

對於我來說,這將是更好地隱藏/顯示誤差比,刪除/添加同樣的事情。

HTML

<div id="divmsg6"><p class = 'red'>Incorrect file dimension, try again</p></div> 

CSS

#divmsg6 p.red { 
    display: none; 
} 

發生錯誤時,jQuery的像這樣,

$('#divmsg6 p.red').show(); // show error message. 

,那麼你可能想隱藏一些時間。

$('#divmsg6 p.red').show(function(){ 
    var $this = $(this); 
    setTimeout(function(){ 
     $this.fadeOut(1500); 
    },1500); 
}); 

或其他任何隱藏它的變化都會發生,只是在發生錯誤時再次顯示。

simple demo

3

可以使用html()方法:

$('#divmsg6').html("<p class = 'red'>Incorrect file dimension, try again</p>");