我试图从网址中提取文件名作为ajax调用的一部分,但我无法让它正确地格式化文本。看起来应该很容易,我错过了什么。我查看了对类似问题的回答,但没有任何对我有用的东西......现在正在重复源标记 -
AJAX
var dir = "videos/";
var fileextension = ".mp4";
var videos = $.ajax({
//This will retrieve the contents of the folder if the folder is
configured as 'browsable'
url: dir,
success: function (data) {
//List all .mp4 file names in the page
$(data).find("a:contains(" + fileextension + ")").each(function ()
{
var filename = $(this).attr("href").replace(window.location.host, "").replace("http://", "");
$("#container").append("<div><video id='video1' class='vidarray' preload='none' poster='bkg.png'><src='"+ filename +"'></video></div>");
});
}
});
使用建议的代码后,我添加了新的返回:
<src='comm_7054.mp4'></src='comm_7054.mp4'>
答案 0 :(得分:1)
尝试:
var dir = "videos/";
var fileextension = ".mp4";
var videos = $.ajax({
//This will retrieve the contents of the folder if the folder is
configured as 'browsable'
url: dir,
success: function (data) {
//List all .mp4 file names in the page
$(data).find("a:contains(" + fileextension + ")").each(function ()
{
var filename = $(this).attr("href").replace(window.location.host, "").replace("http://", "");
var video = $('<video />', {
id: 'video1',
class:'vidarray',
preload:'none',
poster: 'bkg.png'
src: filename,
type: 'video/mp4',
controls: true
});
video.appendTo($("#container"));
});
}
});
您还可以为$(this).attr(...
替换$(this).prop(...
他们返回包含href
我使用这个问题修改我的答案:Create a new html5 video object with jQuery,你需要调整它以适应你的需要,但这是正确的方法......