我在 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/