2012-09-20 23 views
1

我有类似在javascript中执行字符串变量的内容作为代码行吗?

_onmouseover”: “this.className = this.className.replace( '悬停', '')”;

我试图像

buttonObject.onmouseover =函数(){ 窗口[this.someObject._ _onmouseover]()执行它; };

而且我不知道它有多可能。

让我告诉你们我的情景。我正在创建这个插件,在jquery对话框中生成四种类型的对话消息。那些是'警告','错误','注意'和'确认'。因此,让我们说在dom中有4个跨度应该触发这四个。

<span id='DialogueWarning'> Warning </span> 
<span id='DialogueError'> Error </span> 
<span id='DialogueNote'> Note </span> 
<span id='DialogueConfirm'> Confirm </span> 

现在让hadle点击表示对话

jQuery('#DialogueWarning').click(function(){ 

     var dialogue = new Dialogue({ 
      "type":"Warning", 
      "message":"Are you sure you want to close this window without saving your changes?", 
      "buttons": 
      [ 
       { 
        "text":"Close without saving", 
        "_onmouseover": "this.className+=' hover'", 
        "_onmouseout":"this.className=this.className.replace(' hover', '')", 
        "bClass":"e_customButton" 
       }, 

       { 
        "text":"Don't Close", 
        "_onmouseover": "this.className+=' hover'", 
        "_onmouseout":"this.className=this.className.replace(' hover', '')", 
        "bClass":"e_customButton" 
       } 
      ], 
      "closeOnBackgroundClick" : true 
     }); 
    }); 

参见 “_onmouseover” 和_onmouseout啄,我需要这些。有什么办法可以通过其他方式

+0

你可以在对话中显示按钮的处理吗? – Anoop

回答

1

如果你需要一个eval,我敢打赌你在你的应用程序的设计中有一些问题。
例如你可以避免这样的事情:

// ... 
var eventHandlers = { 
    "_onmouseover" : "this.className=this.className.replace(' hover', '')" 
}; 

// ... 

eval(eventHandlers._onmouseover); 

,只是不喜欢它

var eventHandlers = { 
    _onmouseover: function(e) { 
     this.className=this.className.replace(' hover', ''); 
    } 
}; 

buttonObject.onmouseover = eventHandlers._onmouseover; 

有些文章阅读:

# 1
# 2
# 3

0

您可以使用Functiondemo

buttonObject.onmouseover = Function(window [ this.someObject.__onmouseover ]); 
0

为什么它必须是一个字符串呢?

如果你有这样的事情:

var someObject = { 
    _onmouseover: function() { 
     this.className = this.className.replace(' hover', ''); 
    } 
} 

你可以执行它想:

buttonObject.onmouseover = someObject.__onmouseover; 

如果您需要this是按钮对象,你可以做这样的事情:

buttonObject.onmouseover = function() { 
    someObject.__onmouseover.call(buttonObject); 
}; 
+0

我认为'onclick'字符串来自JSON编码回复或其他。 – Chris

+0

@ Abody97是什么让你相信? – dqhendricks

+0

''_onmouseover“:”this.className = this.className.replace('hover','')“;'看起来像JSON-ish。 – Chris