2016-12-01 62 views
0

刚刚学习jQuery,当我使用append时无法将我的变量变为src。它根本不起作用,或者当我查看控制台时,我只是获取变量名的字符串表示形式。jQuery附加变量作为src

这是我的问题的代码:

var $googleURL = "http://maps.googleapis.com/maps/api/streetview?size=600x300&location="+$googleStreet+","+$googleCity; 

$($body).append('<img src='$googleURL'></img>'); 

我不想使用attr因为没有img代码的网页时,只需body标签。我在哪里误入歧途?

+0

您使用'+'到Concat的jQuery中的变量。另外,在jQuery中不需要使用'$'作为变量前兆。可能值得阅读:http://stackoverflow.com/questions/205853/why-would-a-javascript-variable-start-with-a-dollar-sign – junkfoodjunkie

回答

2

请尽量

$($body).append("<img src='"+ $googleURL + "'></img>"); 
1

使用JavaScript,您可以使用+

像这样把里面的字符串变量:"string, " + variable + ", more string"

试试这个代码,它可能会因工作你想要什么去完成。

var googleURL = 'http://maps.googleapis.com/maps/api/streetview?size=600x300&location='+googleStreet+','+googleCity; 

$($body).append('<img src="' + googleURL + '"></img>');