我有一個使用簡單模板構建的網站; header.tpl,navigation.tpl,body_home.tpl,body_about.tpl,body_anotherpage.tpl等。navigation.tpl包含jQuery,並用於動態構建下拉導航菜單。當第一個下拉菜單中單擊一個元素時,下一個元素將根據單擊的元素構建。在某些時候,沒有更多的下拉菜單並設置了變量,例如:var action =「dropdowncomplete」。現在,在body _ *。tpl模板文件中,當動作===「dropdowncomplete」的計算結果爲true時,會運行更多的jQuery。但是,我不知道如何做這個檢查與jQuery(也沒有與JS)。這裏有一個簡單的代碼來說明它是如何工作:在另一個<script>塊中檢查JS變量值
/* header.tpl (<head>) - setting this global variable */
var action = null;
/* navigation.tpl - for simplicity's sake, when link is clicked, the var is set */
$(document).ready(function() {
$('a').live('click', function() {
action = "dropdowncomplete";
}); });
/* body_*.tpl - this should be executed when the var is set, in this case when a link is clicked */
if(action === "dropdowncomplete") {
// do something
}
注意,所有上述3件JS/jQuery代碼是在單獨的腳本塊!
謝謝。
知道這可能是一個錯誤,我經歷了幾次代碼,並找到了一個代碼中的「bug」。這些鏈接在href屬性中有用於生成下一個下拉菜單的信息(它們包含foo = bar之類的信息,並通過AJAX傳遞給腳本),所以我必須在navigation.tpl中使用「return false」 $('a')。live('click',function(){});阻止鏈接重定向到該URL,並且該行破壞了代碼的執行,因此以下腳本塊不會執行。現在我需要解決這個問題。 – TheMagician 2009-10-13 23:22:40
最好將其作爲單獨的問題發佈。這個人的回答。 :) – 2009-10-13 23:26:54