最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Use jQuery to control video tag - Stack Overflow

programmeradmin2浏览0评论

So, I want to use a jQuery function to collect a URL from a link's REL, and pass it to a element.

Collecting the REL and sending it to the is no problem... but what's required to trigger the element's load and play functions, from jQuery?

Here's what I have so far:

$(function(){   
    $('aponentLink').click(function() {
        event.preventDefault();
        var vidURL = $(this).attr('rel');
        $('#myVideo > source').attr('src', vidURL);
        $('#myVideo').load();
        $('#myVideo').play();
    })
});

ATM, it doesn't play... I assume jQuery doesn't natively have access to the play function... but there must be a way around that.

So, I want to use a jQuery function to collect a URL from a link's REL, and pass it to a element.

Collecting the REL and sending it to the is no problem... but what's required to trigger the element's load and play functions, from jQuery?

Here's what I have so far:

$(function(){   
    $('a.componentLink').click(function() {
        event.preventDefault();
        var vidURL = $(this).attr('rel');
        $('#myVideo > source').attr('src', vidURL);
        $('#myVideo').load();
        $('#myVideo').play();
    })
});

ATM, it doesn't play... I assume jQuery doesn't natively have access to the play function... but there must be a way around that.

Share Improve this question edited Oct 30, 2010 at 17:00 Rob 6,87114 gold badges54 silver badges91 bronze badges asked Oct 30, 2010 at 16:42 Benjamin AllisonBenjamin Allison 2,1543 gold badges31 silver badges57 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 20

You need to call .play() on the element itself, not on the jQuery selection. Do the following:

$(function(){   
    $('a.componentLink').click(function() {
        event.preventDefault();
        var vidURL = $(this).attr('rel');
        $('#myVideo').attr('src', vidURL);
        var video = $('#myVideo').get(0);
        video.load();
        video.play();
    })
});
发布评论

评论列表(0)

  1. 暂无评论