2012-10-02 88 views
0

我有誰創造一個彈出延遲3000,並出現在我的網站的腳本,問題是,我不能刪除它,這裏是我的腳本無法關閉JS彈出

HTML

<div id="growl"></div> 

CSS

#growl { 
position: absolute; 
padding:5px; 
bottom: 0; 
right: 5px; 
width: 320px; 
z-index: 10; 
} 

.notice { 
position: relative; 
min-height: 30px; 
padding:5px; 
} 

.skin { 
position: absolute; 
background-color: #000000; 
bottom: 0; 
left: 0; 
opacity: 0.6; 
right: 0; 
top: 0; 
z-index: -1; 
-moz-border-radius: 5px; -webkit-border-radius: 5px; 
} 

按鈕關閉

.close { 
background: transparent url('../img/egrowl.png') 0 0 no-repeat; 
text-indent: -9999px; 
position: absolute; 
top: 2px; 
right: 2px; 
    width: 26px; 
height: 26px; 
} 

我的腳本

$(document).ready(function(){ 

延遲

setTimeout(function() { 

addNotice('<p>Do not Forget To Become A member </p><a href="subscribe.php">Subscribe</a>'); 

},3000); 

關閉功能

$('#growl') 
.find('.close') 
.on('click', function() { 
    $(this) 
     .closest('.notice') 
     .animate({ 
      border: 'none', 
      height: 0, 
      marginBottom: 0, 
      marginTop: '-6px', 
      opacity: 0, 
      paddingBottom: 0, 
      paddingTop: 0, 
      queue: false 
     }, 2000, function() { 
      $(this).remove(); 
     }); 
    }); 
     }); 

設置

function addNotice(notice) { 
$('<div class="notice"></div>') 
    .append('<div class="skin"></div>') 
    .append('<a href="#" class="close">close</a>') 
    .append($('<div class="content"></div>').html(notice)) 
    .hide() 
    .appendTo('#growl') 
    .fadeIn(1000); 
} 

回答

1

有更多的東西在你的設置中是錯誤的。我創造了這個小提琴: http://jsfiddle.net/CyJRF/2/

你要綁定一個click事件到「.close」元素,而是你在$做它(文件)。就緒(),已內創建元素之前「 addNotice」。 我已經移動了一些javascript ...

而@Jordan正確指出,您需要更改$(this)。我現在只是使用$("#growl .notice")

+0

非常感謝,這是偉大的,它在JSFIDDLE工作正常,但是當我把它放在我的HTML,我有這個錯誤意外的標記非法結尾。 –

+1

檢查此帖:http://stackoverflow.com/questions/4404526/unexpected-token-illegal-in-webkit 或只是谷歌'意外的令牌非法';) –

+0

對不起,我的不好,謝謝一切都工作完美謝謝你很非常。 –

4

的這個在點擊回調函數不再是指調用對象,所以你需要調用對象的這種情況下綁定到功能,或將其更改爲的id您要關閉的元素。