2012-07-20 89 views
0

這可能是一個簡單的問題,但我在這裏感到困惑。在jQuery語句中插入變量

var videoSrc = "video/sample.mp4"; 
$('.abc').append('<video width="650" height="300" controls="controls"> <source src= "' 
+ videoSrc 
+ 'type="video/mp4" id="myVideo"/>Your browser does not support the video tag.</video>'  
); 
}); 

我想在src屬性中插入變量videoSrc的值。它不這樣工作。

下面的代碼工作正常:

$('.abc').append('<video width="650" height="300" controls="controls"> <source src="videos/sample2.mp4" ' 
+'type="video/mp4" id="myVideo"/>Your browser does not support the video tag.</video>'  
); 

但我不得不動態插入src屬性的值。

幫我解決這個問題。

感謝

回答

0

這一個工作:

var videoSrc = "video/sample.mp4"; 
$('.abc').append('<video width="650" height="300" controls="controls"> <source src= " ' 
+ vidSrc 
+ ' " type="video/mp4" id="myVideo"/>Your browser does not support the video tag.</video>'  
); 

謝謝大家。

3

你沒有關閉報價爲您src屬性,並作爲@Luuk麪包車Egeraat指出,你有多餘的「});」最後。

var videoSrc = "video/sample.mp4"; 
$('.abc').append('<video width="650" height="300" controls="controls"> <source src= "' 
+ videoSrc 
+ 'type="video/mp4" id="myVideo"/>Your browser does not support the video tag.</video>'  
); 

成爲

var videoSrc = "video/sample.mp4"; 
$('.abc').append('<video width="650" height="300" controls="controls"> <source src= "' 
+ videoSrc 
+ '" type="video/mp4" id="myVideo"/>Your browser does not support the video tag.</video>'  
); 
+1

這將仍然失敗,代碼是錯誤的,因爲'});' – 2012-07-20 16:36:50

+0

@LuukvanEgeraat謝謝,修正。 – 2012-07-20 16:38:57

1

只是一些語法錯誤,here is a working version

var videoSrc = "video/sample.mp4"; 

$('.abc').append('<video width="650" height="300" controls="controls"><source src= "' + videoSrc + '" type="video/mp4" id="myVideo"/>Your browser does not support the video tag.</video>');​ 
+0

它也沒有工作。 – jeewan 2012-07-20 16:55:28

+0

............... – jeewan 2012-07-20 16:58:30

+0

該代碼是有效的,但該文件不存在,因此不會播放。 – 2012-07-20 17:11:26