1

谁能帮我把这个 jQuery 代码转换成 JavaScript?因为我正在使用的库不能使用 jQuery,而且我对此一无所知。

这里是:

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

谢谢。

4

2 回答 2

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();
    };
}
于 2013-11-09T09:36:04.107 回答
0

在 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();
    };
}
于 2013-11-09T09:34:02.607 回答