2012-02-13 25 views
0

我想將jw播放器添加到自定義cms中,用戶可以在其中添加mp4文件並將其作爲鏈接出現在頁面中。但我想要做的是檢測頁面上的每個mp4文件,並讓jw播放器將每個mp4文件包裝在頁面上並允許播放它。這可能嗎?如果是這樣的話,就有一個這樣的例子。檢測頁面上的mp4文件並自動將它們包裝在jwplayer中?

+1

你可以給你的HTML結構的例子嗎? – 2012-02-13 15:57:12

回答

0

假設渲染的結果是某種含有像錨標記列表:

<a href="file1.mp4">File1</a> 
<a href="file2.mp4">File2</a> 
... 

您可以分析每一個錨標記和正則表達式,它的href屬性的內容,即:

$("a").each(function() { 
    var href = $(this).attr('href'); 

    if(href.search(/*\.mp4/) != -1) { 
    // this is an anchor tag where the href attribute points to an mp4 file 
    // so you can replace it with some other html code 

    // for instance, hide the anchor tag 
    $(this).hide(); 

    // add some other html code after the hidden anchor tag, eg. jw player 
    $(this).after('<div class="player"> <-- player code goes here --> </div>'); 
    } 
}); 

它甚至如果您可以修改正在呈現的模板,那麼服務器端確定應該替換哪些鏈接的方式更容易。例如,如果你能產生類似:

<a hreF="file1.mp4" class="mp4-file">File1</a> 
... 

你可以只是做

$(".mp4-file").each(function() { 
    // no need to regex here 

    $(this).hide(); 
    $(this).after('<div class="player"> <-- player code goes here --> </div>'); 
}); 

我覺得你的想法...

+0

是的,我明白了!謝謝 – user794846 2012-02-14 09:05:43

相關問題