我有一个 html 文档,有 2 个 div(左和右),并且该文档加载了一些 js 函数,一个名为 editme 的函数通常会在左侧 div 中加载,但是当用户展开右侧导航时,我想要这个 editme启动/打开一个新窗口并执行操作的功能,这意味着 如果可能,以下选项之一将起作用: 1)销毁现有函数并重新创建同名新函数 2)动态更改js函数
哪些是可能的以及如何实现?
谢谢。
根据建议添加了以下内容:
edit=function(){} // works, however less value
edit=function(id) { 'window.open("editMe.cfm?taid="+id,"edit","left=260,top=20,height=410,width=590,resizable=1,scrollbars=1")' } // fails
SyntaxError: unterminated string literal
attempted to scape the double quotes like the following to no avail, 'window.open(\"editMe.cfm?taid=\"+id,\"edit\",\"left=260,top=20,height=410,width=590,resizable=1,scrollbars=1\")'
我做错了什么吗?
仅供引用,此代码是 onclick 事件其他代码的一部分。
最佳答案
函数是 JavaScript 中的一流对象。只需将新函数重新分配给您已经(隐式)创建的变量...
function foo() {
//do stuff
}
//now, reassign the var to do something else
foo = function() {
//do other stuff
}
并不是说这是一个好的做法...但要回答你的问题,是的,这是可能的。
关于javascript - 动态更改 javascript 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16197212/