编程语言
首页 > 编程语言> > javascript-如何将&autoplay = 1动态添加到youtube嵌入代码?

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

标签:youtube,dynamic,autoplay,javascript,jquery
来源: https://codeday.me/bug/20191102/1992505.html