用变量定义id的Jquery

用变量定义id的Jquery,jquery,Jquery,你们都知道,这很好用 $("#foo").html = "bar"; 但是我该怎么做呢 var i = "foo"; $(i).html= "bar"; 我也尝试过$(“#”+I),但这也不起作用。这不起作用 $("#foo").html = "bar"; //Overwrites the object's html function with a string 这应该是 $("#"+i).html("bar"); //Sets the inner HTML of the selected

你们都知道,这很好用

$("#foo").html = "bar";
但是我该怎么做呢

var i = "foo";
$(i).html= "bar";
我也尝试过
$(“#”+I)
,但这也不起作用。

这不起作用

$("#foo").html = "bar"; //Overwrites the object's html function with a string
这应该是

$("#"+i).html("bar"); //Sets the inner HTML of the selected element.

我不知道你想干什么

但是如果要在
id='foo'

你可以用

   var i = "foo" ;

   $('#'+i).html("bar");

是的,你说得对,我需要的是括号而不是
=
。我把这两个搞混了。愚蠢的错误。谢谢