Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/453.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript getElementById不返回null_Javascript_Html - Fatal编程技术网

Javascript getElementById不返回null

Javascript getElementById不返回null,javascript,html,Javascript,Html,我在jquery中有以下功能: $('canvas').createWindow('xyz', 500, 600); 而js背后的代码是: var $ = function(element) { if (this.constructor !== $) { return new $(element); } alert(element); var windowObj = document.ge

我在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就绪还是window onload上执行此操作的?您是否有ID为
canvas
的元素?您没有将nodename与id混淆?

您的代码很好确保元素是id

您的HTML中是否有该id的元素?
element
是一个字符串吗?我猜文档中没有该id的元素?我的HTML中有element.toString()不起作用。$('canvas').createWindow('xyz',500,600);好啊您在它存在之前调用它。@pbcoder-如果它解决了您的问题,请不要忘记选择一个答案。