简体   繁体   English

将jQuery代码转换为JavaScript

[英]convert jquery code to javascript

I have a jquery script that i want to convert in javascript. 我有一个要在javascript中转换的jquery脚本。

Jquery: jQuery:

<script type="text/javascript">
    $(document).ready( function() {

        addCssTransform();

        $( window ).resize(function() {
            addCssTransform();
        });

        function addCssTransform() {
            var docWid = $(document).width();
            var w = (window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth);
            var actualWidth = w - (w - docWid);

            var zoom = actualWidth / 1280;
            var zoomRatio = zoom *100;
            console.log(zoomRatio);
            if(actualWidth > 1280) {
                $('html').css( 'font-size', + zoomRatio+ '%' ); /* IE 9 */

            }
        }
    });
</script> 

I have tried and here is the output. 我试过了,这里是输出。 But it is not working and giving error in console. 但是它不起作用并且在控制台中给出错误。

javascript: javascript:

<script type="text/javascript">
 addCssTransform();

      window.addEventListener('resize', function(event){
            addCssTransform();
        });

        function addCssTransform() {
            var docWid = document.body.clientWidth;
            var w = (window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth);
            var actualWidth = w - (w - docWid);

            var zoom = actualWidth / 1280;
            var zoomRatio = zoom *100;
            console.log(zoomRatio);
            if(actualWidth > 1280) {
                document.getElementsByTagName("html").style.fontSize = zoomRatio + '%';
                //$('html').css( 'font-size', + zoomRatio+ '%' ); /* IE 9 */

            }
        };
    </script>

It seems there is an error in line: 似乎在一行中有一个错误:

document.getElementsByTagName("html").style.fontSize = zoomRatio + '%';

getElementsByTagName returns an array-like NodeList object , not an element. getElementsByTagName返回类似数组的NodeList对象 ,而不是元素。 You can't set style on it. 您无法在其上设置style You have to loop over it and set the style on each member (which will be elements) of it in turn. 您必须遍历它,并依次在其每个成员(将是元素)上设置样式。

<script type="text/javascript">
    // Add 'DOMContentLoaded' event
    document.addEventListener('DOMContentLoaded', function () {

        addCssTransform();

        // NOTE: Just reference the function. Don't create new one unless needed
        window.addEventListener('resize', addCssTransform, false);

        function addCssTransform() {
            var docWid = document.body.clientWidth;
            var w = (window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth);
            var actualWidth = w - (w - docWid);

            var zoom = actualWidth / 1280;
            var zoomRatio = zoom *100;
            console.log(zoomRatio);
            if(actualWidth > 1280) {

                // If you are adding styles to 'html' element, use available 'document.documentElement' property
                document.documentElement.style.fontSize = zoomRatio + '%';
                //$('html').css( 'font-size', + zoomRatio+ '%' ); /* IE 9 */

            }
        }
    }, false);
    </script>
  • Add DOMContentLoaded event and place your JavaScript inside it. 添加DOMContentLoaded事件并将JavaScript放入其中。
  • Reference the function. 引用功能。

You can write the below code 您可以编写以下代码

window.addEventListener('resize', function (event) {
    addCssTransform();
}, false);

as

window.addEventListener('resize', addCssTransform, false);
  • Use document.documentElement to access 'html' element 使用document.documentElement访问'html'元素

getElementsByTagName returns an nodeList over which you would need to loop. getElementsByTagName返回需要循环的nodeList。

An alternative would be to use querySelector which returns a single element instead: 一种替代方法是使用querySelector ,它返回单个元素:

document.querySelector('html');

You are calling the addCSSTransform function before it is defined. 在定义之前,您正在调用addCSSTransform函数。 Move the call to after the function declaration, ie: 将调用移到函数声明之后,即:

    window.addEventListener('resize', function(event){
        addCssTransform();
    });

    function addCssTransform() {
        var docWid = document.body.clientWidth;
        var w = (window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth);
        var actualWidth = w - (w - docWid);

        var zoom = actualWidth / 1280;
        var zoomRatio = zoom *100;
        console.log(zoomRatio);
        if(actualWidth > 1280) {
            document.getElementsByTagName("html").style.fontSize = zoomRatio + '%';
            //$('html').css( 'font-size', + zoomRatio+ '%' ); /* IE 9 */

        }
    };

    addCssTransform();

as suggested by Vigneswaran, you may wish to bind the call to a DOMContentLoaded event (equivalent to $(document).ready ) as follows: 如Vigneswaran所建议,您可能希望将调用绑定到DOMContentLoaded事件(相当于$(document).ready ),如下所示:

document.addEventListener('DOMContentLoaded', addCssTransform);

The suggestions above regarding nodeLists returned from getElementsByTagName are also correct (the clue's in the name - getElement s ByTagName). 上面关于从getElementsByTagName返回的nodeLists的建议也是正确的(提示中的线索 -getElement的ByTagName)。 There will (or should!) only ever be one html element, so you can safely replace document.getElementsByTagName("html").style with document.getElementsByTagName("html")[0].style 只会(或应该!)只有一个html元素,因此您可以用document.getElementsByTagName("html")[0].style安全地替换document.getElementsByTagName("html").style

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM