javascript-如何将&autoplay = 1动态添加到youtube嵌入代码?

这是我网站上youtube嵌入代码的示例:

<object width="525" height="320"><param name="wmode" value="opaque">
    <param name="movie" value="http://www.youtube.com/v/yciTqWafKPU?version=3">
    <param name="allowFullScreen" value="true">
    <param name="allowscriptaccess" value="always">
    <embed src="http://www.youtube.com/v/yciTqWafKPU?version=3" type="application/x-shockwave-flash" width="525" height="320" allowscriptaccess="always" allowfullscreen="true" wmode="opaque">
</object>

我相信我需要为此添加& autoplay = 1:< param name =“ movie” value =“ http://www.youtube.com/v/yciTqWafKPU?version=3\u0026amp;autoplay=1”>并且:< embed src =“ http://www.youtube.com/v/yciTqWafKPU?version=3\u0026amp;autoplay=1” type =“ application / x-shockwave-flash” width =“ 525” height =“ 320“ allowscriptaccess =” always“ allowfullscreen =” true“ wmode =” opaque“>

我需要使用JQuery动态地做到这一点.我该怎么做?

解决方法:

没有太多困难,我本以为…

$('param[name="movie"]').val(function(i, oldVal) {
    return oldVal + (oldVal.indexOf('?') ? '&autoplay=1' : '?autoplay=1');
});
$('embed').prop('src', function(i, oldSrc) {
    return oldSrc + (oldSrc.indexOf('?') ? '&autoplay=1' : '?autoplay=1');
}

您可能希望做一个更具体的选择器,例如$(’embed [src ^ =“ http://www.youtube.com/”]’)或$(‘object param [name =“ movie”] ‘)-这取决于您的页面在其他位置包含的内容…

请参阅API参考:

> attribute-equals selector
> val
> prop

上一篇:javascript-使用jQuery播放/停止YouTube多个iframe播放器


下一篇:JavaScript-YouTube API问题