2013-03-27 28 views
0

我對這個非常初學者,想反正是有,我可以添加這兩個值如何在Javascript上添加值?

maxScreenWidth: 480, 
menuTitle: 'Menu:' 

到這個腳本。

function DropDown(el) { 
    this.mainNav = el; 
    this.initEvents(); 
} 
DropDown.prototype = { 
    initEvents: function() { 
     var obj = this; 

     obj.mainNav.on('click', function (event) { 
      $(this).toggleClass('active'); 
      event.stopPropagation(); 
     }); 
    } 
} 

$(function() { 

    var mainNav = new DropDown($('#mainNav')); 

    $(document).click(function() { 
     // all dropdowns 
     $('.dropdown-menu').removeClass('active'); 
    }); 

}); 

在此先感謝。

  • 此外,這是我申請到我的網站的下拉菜單。

http://tympanus.net/Tutorials/CustomDropDownListStyling/index2.html

我想只爲佈局手機應用此菜單,但保持其形狀,不管大小是什麼屏幕。它應該消失,當它是更多的480px,但它不是。

非常感謝您的幫助。

+0

您是否希望在調用偵聽器時應用它們?如果是這樣,只需將它們添加到相關的CSS規則(即* active *類)。 – RobG 2013-03-27 05:44:43

回答

0

如果要添加這些屬性,只需添加它們象下面這樣:

function DropDown(el, width, title) { 
    this.mainNav = el; 
    this.maxScreenWidth = width; //added 
    this.menuTitle = title; //added 
    this.initEvents(); 
} 

現在,他們是你的構造的一部分,可通過參數

然後當你打電話給你的構造,只是通過什麼樣的值應該是

$(function() { 
    var mainNav = new DropDown($('#mainNav'), 480, 'Menu'); //See, pass them here. 

    $(document).click(function() { 
     // all dropdowns 
     $('.dropdown-menu').removeClass('active'); 
    }); 

}); 
+0

謝謝,但它不起作用。也許問題在別的地方。 – Kim 2013-03-27 06:10:38

+0

@ user2214314編輯併發布您在問題中遇到的問題 – SomeShinyObject 2013-03-27 06:12:01

+0

我只需輸入更多特定信息。非常感謝你的幫助。 – Kim 2013-03-27 06:27:13

0

可以作爲下拉參數添加此值Christoper寫道,你也可以創建全局變量:

var maxScreenWidth = 480; 
var menuTitle = 'Menu:'; 
function DropDown(el) { 
    this.mainNav = el; 
    this.initEvents(); 
} 
//... 

不過,如果你把它寫在你的js文件,其他代碼可以訪問和更改您的全局變量(他們畢竟:)全球),所以這種技術存在:

(function ($) { 
    var maxScreenWidth = 480; 
    var menuTitle = 'Menu:'; 
    function DropDown(el) { 
     this.mainNav = el; 
     this.initEvents(); 
    } 
    //... 
}(jQuery)); 

在最後一個例子是你用'private scope'創建函數,所以你的'private'變量不能從其他js代碼訪問。你也應該注意到你不能從你的應用程序中的其他代碼訪問DropDown,只能在這個函數中。