javascript - getElementById 确实返回 null

标签 javascript html

我在 jquery 中有以下函数:

$('canvas').createWindow('xyz', 500, 600);

后面的js代码是:

    var $ = function(element) {
        if (this.constructor !== $) {
                return new $(element);
        }

        alert(element);
        var windowObj = document.getElementById(element);

        this.createWindow = function(src, width, height) {          
            if(width != "" && height != "") {
                windowWidth = windowObj.style.width = width + 'px';
                windowHeight = windowObj.style.height = height + 'px';
            }
        };
    };

但问题是 js 说 windowObj 是 null,但 alert(element) 工作正常!有任何想法吗?

最佳答案

你是在 DOM 就绪还是窗口加载时执行此操作的吗?您是否有 ID 为 canvas 的元素?您没有混淆节点名和 id 吗?

关于javascript - getElementById 确实返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4564929/

相关文章:

javascript - Jquery 如果没有 -

javascript - 如何在 AngularJS 中使用 ng-change 将输入类型 'time' 转换为字符串?

html - 父级 flexbox 基于另一个 flexbox 增长/收缩

javascript - 如何以编程方式将内容脚本添加到 iframe 文档

javascript - 电话号码验证,其中包含 +** ***-***-****(再加上两位数的国家/地区代码和 10 位数的手机号码)

html - 如何在CSS中进行硬币翻转变换

javascript - 如何在 html canvas 中用颜色填充一定比例的圆圈区域?

html - 使用 Bootstrap 响应式网格系统时如何为图像提供动态宽度和高度

javascript - 带有加载通知的跨浏览器 xmlhttprequest 响应

javascript - 整数被添加到数组中