我想執行在腳本開始時定義的函數,讓我們調用此函數進行初始化。這個函數也使用了一個變量,讓我們稱之爲登錄,它是在定義了變量login之後由一個包含我的jquery腳本文件的php文件定義的。除非調整大小或點擊功能,否則不會加載函數
PHP/HTML:
<script type="text/javascript">
login = '<?php echo $login; ?>';
...
</script>
<!-- script is included -->
<script type="text/javascript" src="script.js"></script>
的jQuery:
function initialize(){
$("." + login).remove();
}
jQuery.moreContent = function moreContent()
{
//more content is loaded
...
initialize();
}
那麼moreContent功能加載,我可以看到更多的內容出現在我的屏幕上,但initialiye沒有加載。只有當我使用像大小調整功能(在的script.js文件末尾),它的工作原理
的jQuery(在腳本的末尾):
//load function initialize
initialize();
//this function doesnt work too, I tested if it even loads and it does (used a number that was increased by one whenever function was loaded which actually happened...)
//however the wished element with the class of variable login is not removed
//resize function
$(window).resize(initialize);
//this one suddenly works
...
我不知道爲什麼突然有工作其它功能,以及爲什麼它在其它情況下不工作
您需要等到該文件是訪問'$前準備好( 「」 +登錄)'元素,更多的細節[這裏](https://learn.jquery.com/using-jquery-核心/文檔就緒/) – Titus
我做過...我甚至試圖將類更改爲登錄的價值,這也沒有幫助:/ – Moritz
可能是初始化函數被覆蓋在jquery範圍內嘗試更改名稱一段時間。 – Viney