简体   繁体   English

如何在Javascript上添加值?

[英]How to add values on Javascript?

I'm a very beginner on this and wondering is there anyway that I can add these two values 我是一个非常初学者,想知道是否我可以将这两个值相加

maxScreenWidth: 480,
menuTitle: 'Menu:'

into this script. 进入这个脚本。

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');
    });

});

thanks in advance. 提前致谢。

  • In addition, this is the dropdown menu that I'm applying to my website. 另外,这是我要应用到我的网站的下拉菜单。

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

I'm trying to apply this menu only for phone layout but it maintains its form no matter what screen size is. 我试图将此菜单仅应用于电话布局,但无论屏幕大小如何,它都保持其形式。 It is supposed be disappeared when it's more 480px but it isn't. 480像素以上的像素应该消失了,但事实并非如此。

Thank you so much for your help. 非常感谢你的帮助。

If you want to add those properties, just add them like below: 如果要添加这些属性,只需如下添加它们:

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

Now they are part of your constructor as passable arguments 现在它们作为可传递参数已成为构造函数的一部分

Then when you call you constructor, just pass what those values should be 然后,当您调用构造函数时,只需传递这些值应为

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

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

});

You can add this values as DropDown parameters as Christoper wrote, also you can create global variables: 您可以像Christoper所写的那样将这些值添加为DropDown参数,也可以创建全局变量:

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

But then, if you write it in your js file, other code could access and change your global variables (they are global after all :) ), so this technique exists: 但是,如果您将其写入js文件中,则其他代码可以访问和更改全局变量(毕竟它们是全局的:)),因此该技术存在:

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

In the last example you'r creating function with 'private scope', so your 'private' variables ain't accessible from other js code. 在上一个示例中,您正在使用“私有范围”创建函数,因此您的“私有”变量无法从其他js代码访问。 Also you should note that you couldn't access DropDown from other code in you application, only within this function. 还应注意,仅在此功能内,无法从应用程序中的其他代码访问DropDown。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM