如何使用jquery将变量传递到分配给元素的html中
我正在尝试这样做:如何使用jquery将变量传递到分配给元素的html中,jquery,Jquery,我正在尝试这样做: $('#player-container').html('<div id="film-player" class="flowplayer play-button is-splash" style="background:#777 url(myVariable) no-repeat;">'); $(“#播放器容器”).html(“”); html没有显示,因为它不在这里的代码块中。基本上,我试图在动态分配给元素的html中嵌套这样一个变量:style等于bac
$('#player-container').html('<div id="film-player" class="flowplayer play-button is-splash" style="background:#777 url(myVariable) no-repeat;">');
$(“#播放器容器”).html(“”);
html没有显示,因为它不在这里的代码块中。基本上,我试图在动态分配给元素的html中嵌套这样一个变量:style等于background url(myVariable) 由于字符串是以单引号开头的,因此您可以再次使用
'
和+
如下连接:url(“+myVariable+”)
因此,您的代码应该是:
$('#player-container').html('<div id="film-player" class="flowplayer play-button is-splash" style="background:#777 url('+myVariable+') no-repeat;">');
$(“#播放器容器”).html(“”);
有关连接运算符(javascript)的详细信息,请参见,因为您使用单引号开始字符串,所以再次使用
'
和+
进行如下连接:url(“+myVariable+”)
因此,您的代码应该是:
$('#player-container').html('<div id="film-player" class="flowplayer play-button is-splash" style="background:#777 url('+myVariable+') no-repeat;">');
$(“#播放器容器”).html(“”);
更多关于连接运算符(javascript)的信息,请访问谢谢,我只是自己做的,而你回答说,非常好@温迪,太好了!或许您可以单击以接受答案;)哎呀,我以为我做到了!投了赞成票,这就是当你点击太快时会发生的情况…谢谢,我只是自己做的,而你回答说,很好@温迪,太好了!或许您可以单击以接受答案;)哎呀,我以为我做到了!投了赞成票,这就是当你点击太快时发生的事情。。。