Jquery使用变量定义id

jdb*_*org -1 jquery

众所周知,这样做很好

$("#foo").html = "bar";
Run Code Online (Sandbox Code Playgroud)

但是我该怎么做呢?

var i = "foo";
$(i).html= "bar";
Run Code Online (Sandbox Code Playgroud)

我也试过$("#"+i)了,但那也行不通.

Den*_*nis 6

这不起作用.

$("#foo").html = "bar"; //Overwrites the object's html function with a string
Run Code Online (Sandbox Code Playgroud)

这应该.

$("#"+i).html("bar"); //Sets the inner HTML of the selected element.
Run Code Online (Sandbox Code Playgroud)