content
을 ID에서 클래스로 변경 한 이유는 하나 이상의 콘텐츠 영역이 있다고 생각했기 때문입니다.
나는 훨씬 더 효율적인 방법이 있다고 확신하지만, 이것에 대한 나의 시도이다. 참고 나는 정규식을 먹었으므로 내가 가지고있는 것만 큼 가까이 갈 수있다. 누군가가 그것을 향상시킬 수 있다고 확신하기 때문에 각 루프 안에 ?v=
을 대신 할 필요가 없다.
HTML
<div class="content"><p>Here is a cool video. Check it out: http://www.youtube.com/watch?v=oHg5SJYRHA0 and this one http://www.youtube.com/watch?v=fLBPsZVI8Gc&feature=player_embedded</p></div>
<div class="content"><p>Here is a cool video. Check it out: http://www.youtube.com/watch?v=fLBPsZVI8Gc&feature=player_embedded</p></div>
스크립트
$(document).ready(function(){
// I added the video size here in case you wanted to modify it more easily
var vidWidth = 425;
var vidHeight = 344;
$('.content:contains("youtube.com/watch")').each(function(){
var that = $(this);
var txt = $(this).html();
// Tthis could be done by creating an object, adding attributes & inserting parameters, but this is quicker
var e1 = '<obj'+'ect width="' + vidWidth + '" height="' + vidHeight + '"><param name="movie" value="http://www.youtube.com/v/';
var e2 = '&hl=en&fs=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" ' +
'value="always"></param><em'+'bed src="http://www.youtube.com/v/';
var e3 = '&hl=en&fs=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="' + vidWidth +
'" ' + 'height="' + vidHeight + '"></embed></object> ';
var vid = txt.match(/((\?v=)(\w[\w|-]*))/g); // end up with ?v=oHg5SJYRHA0
if (vid.length) {
$.each(vid, function(i){
var ytid = this.replace(/\?v=/,'') // end up with oHg5SJYRHA0
that.append(e1 + ytid + e2 + ytid + e3)
})
}
})
})
나는 꽤 아니다 인정하는 최초의 수 있습니다,하지만 작동합니다. 또한 this pastebin
업데이트에서이 코드의 작업 버전을 붙여 : 나는 코드를 조금 청소했습니다, 여기가 (demo) 지금 모습입니다 :
$(document).ready(function(){
// I added the video size here in case you wanted to modify it more easily
var vidWidth = 425;
var vidHeight = 344;
var obj = '<object width="' + vidWidth + '" height="' + vidHeight + '">' +
'<param name="movie" value="http://www.youtube.com/v/[vid]&hl=en&fs=1">' +
'</param><param name="allowFullScreen" value="true"></param><param ' +
'name="allowscriptaccess" value="always"></param><em' +
'bed src="http://www.youtube.com/v/[vid]&hl=en&fs=1" ' +
'type="application/x-shockwave-flash" allowscriptaccess="always" ' +
'allowfullscreen="true" width="' + vidWidth + '" ' + 'height="' +
vidHeight + '"></embed></object> ';
$('.content:contains("youtube.com/watch")').each(function(){
var that = $(this);
var vid = that.html().match(/(?:v=)([\w\-]+)/g); // end up with v=oHg5SJYRHA0
if (vid.length) {
$.each(vid, function(){
that.append(obj.replace(/\[vid\]/g, this.replace('v=','')));
});
}
});
});
이 질문에 대한 답변은 정규식 부분에 도움이 될 수 있습니다. http://stackoverflow.com/questions/1713222/add js-jquery – beggs
BTW에는 합리적으로 사용하기 쉽고 상당히 강력한 (재생 목록 등) https : // developers의 YouTube 자바 스크립트 API가 있습니다. .google.com/youtube/js_api_reference –