无法正确加载外部js文件

时间:2016-10-19 15:29:29

标签: javascript html

如果我在html页面中使用带有script标记的代码,我有一个动态工具提示的JS文件。但是,当我使用script src标记从源代码中使用它时,脚本会加载,但是当我尝试使用工具提示时,它们不起作用。这是JS代码

var tooltips = document.querySelectorAll('.tooltip div');

window.onmousemove = function (e) {
    var x = (e.clientX + 20) + 'px',
        y = (e.clientY + 20) + 'px';
    for (var i = 0; i < tooltips.length; i++) {
        tooltips[i].style.top = y;
        tooltips[i].style.left = x;
    }
};

1 个答案:

答案 0 :(得分:1)

仅在呈现HTML时运行此脚本。

window.onload = function() {
    var tooltips = document.querySelectorAll('.tooltip div');

    window.onmousemove = function (e) {
        var x = (e.clientX + 20) + 'px',
            y = (e.clientY + 20) + 'px';
        for (var i = 0; i < tooltips.length; i++) {
            tooltips[i].style.top = y;
            tooltips[i].style.left = x;
        }
    };
}
相关问题