2012-08-15 96 views
0

我有一個非常簡單的jQuery代碼(滑動一段文字)基於點擊一個div。jquery不能在wordpress內容中工作

<script> 
jQuery("#test").click(function() { 
    jQuery(this).slideUp(); 
}); 
</script> 

這工作時

<div id="test">some text</div> 

被放置在頁面的主要內容之外,因此在頁眉或頁腳,但是當<div id="test">some text</div>被放置在內容中這是行不通的部分。

我已經在網上搜索了一個解決方案,但是我找不到解決方案。

回答

1

確保是在DOM準備好執行的代碼:

jQuery(document).ready(function() { 
    jQuery("#test").click(function() { 
     jQuery(this).slideUp(); 
    }); 
}); 

說明:http://api.jquery.com/ready/

0

使用$(document).ready函數爲你的代碼的包裝。