有人可以解释一下下面的函数是如何工作的以及我如何创建一个新的按钮对象吗?我需要调用MYAPP.dom.Button函数吗?我假设“return b”返回一个按钮对象?
var MYAPP = {};
MYAPP.dom = {};
MYAPP.dom.Button = function(text, conf) {
var styles = {
font: 'Verdana',
border: '1px solid black',
color: 'black',
background: 'grey'
};
function setStyles() {
for (var i in styles) {
b.style[i] = conf[i] || styles[i];
}
}
conf = conf || {};
var b = document.createElement('input');
b.type = conf['type'] || 'submit';
b.value = text;
setStyles();
return b;
};
最佳答案
你是对的,b
指的是 button
元素。您需要调用MYAPP
对象的方法,如下所示:
MYAPP.dom.Button(x, y);
对于x
,您需要传入一些文本作为按钮的值
,而对于y
,则需要定义 >按钮的类型
。按钮的type
默认为submit
。
下面的MYAPP.dom.Button('hey');
将返回
<input type="submit" value="hey" style="border: 1px solid black; color: black; background: grey;">
正如@dreamweiver 在下面所说的
the
y
parameter is also eligible to hold the style of the button, if not passed it would take the default style properties mentioned inside Button function
关于javascript - 调用函数和命名空间 - JavaScript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41119265/