您可以簡化您的出發DOM
<video id="player" controls="controls" poster="media/poster.jpg" width="540" height="400"></video>
然後你就可以追加JSON來DOM對象
var someJson = [
{
'media_src': 'http://www.test.com/movie.mp4',
'type': 'video/mp4',
'title': 'mp4'
},
{
'media_src': 'http://www.test.com/movie.flv',
'type': 'video/flv',
'title': 'flv'
},
{
'media_src': 'http://www.test.com/movie.ogg',
'type': 'video/ogg',
'title': 'flv'
}
];
// create an object that you can clone and add data to
var sourceObject = $('<source />');
// iterate thru the json
$.each(someJson, function (index_someJson, value_someJson) {
// clone the original object
var thisSourceObject = sourceObject.clone();
// give it attributes based on the json item
thisSourceObject.attr({
'src': value_someJson.media_src,
'type': value_someJson.type,
'title': value_someJson.title
});
// append this to the '#player' element
$('#player').append(thisSourceObject);
})
console.log($('#player')[0])