2011-09-20 68 views
1

我有一个包含一个jQuery命令 字符串我怎样才能执行此字符串?运行jQuery的字符串命令

var myCommand = "$('#update').html('hello world!');"; 
+0

?它来自哪里? –

回答

1

你不应该尝试从字符串执行命令,如果你需要重新使用他们,你应该使用一个功能:

var myCommand = function(){ 
    $('#update').html('hello world!'); 
} 

然后调用它

myCommand(); 

否则,您必须使用eval(),但这不是最佳实践。

或者你可以使用globalEval()哪个更好,因为它为什么你有一个具有JavaScript代码中有一个字符串不使用eval()

0
var myCommand = function(){ 
    $('#update').html('hello world!'); 
} 
$(function() 
{ myCommand(); 
});