2012-09-20 24 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