将简单的jQuery转换为JavaScript

时间:2013-11-09 09:31:39

标签: javascript jquery fullscreen

有人可以将这个jQuery代码转换为JavaScript吗?因为我正在使用的库不能使用jQuery,我对此一无所知。

这是:

$(document).ready(function(){
$('.fs-button').on('click', function(){
    var elem = document.getElementById('fullscreen');
    if (document.webkitFullscreenElement) {
        document.webkitCancelFullScreen();
    } else {
        elem.webkitRequestFullScreen();
    };
}); });

感谢。

2 个答案:

答案 0 :(得分:3)

您需要使用addEventListener

function addEventListenerByClass(className, event, fn) {
    var list = document.getElementsByClassName(className);
    for (var i = 0, len = list.length; i < len; i++) {
        //Attach event
        list[i].addEventListener(event, fn, false);
    }
}

//Iterate and attach event againts class name
addEventListenerByClass('fs-button', 'click', myfunction); 

function myfunction(){
    var elem = document.getElementById('fullscreen');
    if (document.webkitFullscreenElement) {
        document.webkitCancelFullScreen();
    } else {
        elem.webkitRequestFullScreen();
    };
}

答案 1 :(得分:0)

in html

<button onclick="myfunction()" class="fs-button">Click</button>
JS中的

function myfunction(){
 var elem = document.getElementById('fullscreen');
    if (document.webkitFullscreenElement) {
        document.webkitCancelFullScreen();
    } else {
        elem.webkitRequestFullScreen();
    };
}