如何将参数传递给像 left = function() 这样声明的函数
如何将参数传递给声明为 Something = function(){}; 的函数
window.prototype.initInterface = function(){
this.mainPane = document.createElement('div');
this.mainPane.style.border="5px solid grey";
this.mainPane.style.margin="0px";
this.mainPane.style.width="420px";
this.mainPane.style.height="600px";
this.exitButton = document.createElement('input');
this.exitButton.setAttribute("type", "button");
this.exitButton.setAttribute("value", "exit");
this.exitButton.onclick = function(){
document.body.removeChild(this.mainPane);
};
this.mainPane.appendChild(this.exitButton);
document.body.appendChild(this.mainPane);
}
当用户按下退出按钮时,我想从 html 页面的主体中删除 mainPane。
this.exitButton.onclick = function(this.mainPage){
document.body.removeChild(this.mainPane);
};
不起作用
我该怎么做?
How can I pass parameters to a function declared like something = function(){};
window.prototype.initInterface = function(){
this.mainPane = document.createElement('div');
this.mainPane.style.border="5px solid grey";
this.mainPane.style.margin="0px";
this.mainPane.style.width="420px";
this.mainPane.style.height="600px";
this.exitButton = document.createElement('input');
this.exitButton.setAttribute("type", "button");
this.exitButton.setAttribute("value", "exit");
this.exitButton.onclick = function(){
document.body.removeChild(this.mainPane);
};
this.mainPane.appendChild(this.exitButton);
document.body.appendChild(this.mainPane);
}
When the user presses the exit button I want to remove the mainPane from the body of the html page.
this.exitButton.onclick = function(this.mainPage){
document.body.removeChild(this.mainPane);
};
Does not work
How can I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为了让您的
exitButton.onclick
函数能够访问您在封装initInterface
函数中创建的变量,您需要在exitButton.onclick
中创建一个闭包> 函数,返回一个执行您想要的操作的函数并传递该变量。此处了解有关闭包工作原理的更多信息和此处 并查看有效的 小提琴示例。
或者,您忘记闭包,并从触发事件的按钮向上走到 DOM 到您的
mainPane
顺便说一句,如果您这样做,
window.prototype
不存在在浏览器中;window
是浏览器脚本中原型链顶部的对象。您只需要window.initInterface = function () {}
,这与function initInterface() {}
完全相同,因为您在浏览器中使用 javascript 执行的所有操作都会变成窗口
的属性。For your
exitButton.onclick
function to have access to variables you create in the envelopinginitInterface
function you want a to create a closure in theexitButton.onclick
function by returning a function that performs the action you want and passing that the variable.Read more on how closures work here and here and see a working example fiddle.
Alternatively, you forget about closures and walk up the DOM from the button which triggers the event to your
mainPane
As an aside,
window.prototype
does not exist if you are doing this in a browser;window
is the object at the top of prototype chain in browser scripting. You want justwindow.initInterface = function () {}
which is the exact same thing asfunction initInterface() {}
because everything you do in javascript in the browser becomes a property ofwindow
.该函数是不带函数名的函数。它只能使用一次,并且您可能不容易找出应该传递哪些参数。
您可以创建另一个函数,例如:
function go(a1){}
并像 window.prototype.initInterface = go(a1); 那样调用它
或者您可以使用 getDocumentById("DOM ID") 等函数在此未命名函数中获取一些 DOM 参数。
This function is the function w/o function name. It could only be used once and you may not easy to find out what parameters should be passed.
You can create another function like :
function go(a1){}
And call it like window.prototype.initInterface = go(a1);
Or you can get some DOM parameters in this unnamed function by using functions like getDocumentById("DOM ID") etc.