如何在 jQuery 中添加媒体属性

How to add the media attribute in jQuery

本文关键字:添加 媒体 属性 jQuery      更新时间:2023-09-26

我正在使用HTML5和jQuery交互式视频库,其中包含我根据教程改编的Flash回退。该部位是流动的。我不懂jQuery,我正在尝试让小型移动设备播放比当前提供的尺寸(800x450)更小的尺寸(480x272)的视频。

以下是我更改代码的方式,但无法识别媒体查询。我的格式错误是什么?

var videoCode = '<video width="'+480+'" height="'+272+'" controls autoplay autobuffer>
                 <source src="video/'+videoFile+'_small.ogv" type="video/ogg" media="screen and (max-width:500px)">
                 <source src="video/'+videoFile+'_small.mp4" type="video/mp4" media="screen and (max-width:500px)">
                 <video width="'+800+'" height="'+450+'" controls autoplay autobuffer>
                 <source src="video/'+videoFile+'.ogv" type="video/ogg" />
                 <source src="video/'+videoFile+'.mp4" type="video/mp4" />

编辑:固定编码布局

//var videoFile = $(this).attr('videofile');
    var videoFile = (screen.width < 500) ? $(this).attr('videofile') + "_small" : $(this).attr('videofile');
    var videoPoster = $(this).attr('videofile');
    var videoPoster = $(this).attr('videofile');
    var videoWidth = (screen.width < 500) ? 200 : 800;
    var videoHeight = (screen.height < 500) ? 200 : 450;

事实证明,这是解决方案。

您的代码似乎丢失了,但添加/更新元素的属性是$('someselector').attr('attrname', 'value');