Javascript 构造新对象时出现未捕获类型错误
我正在拼凑一些脚本在我的页面上创建浮云Javascript 构造新对象时出现未捕获类型错误,javascript,html,css,null,Javascript,Html,Css,Null,我正在拼凑一些脚本在我的页面上创建浮云 当我试图调用movechip()函数时,问题就出现了 在调用该函数之前(无论是在for循环中还是在构建云的函数中),div和图像被附加到容器div,并且对象包含正确的属性(使用开发工具中的元素检查器进行验证) 我曾尝试将movechip()调用从构造函数移动到for循环,但没有起到任何作用 我删除了原始的样式(使用css),并附加了javascript样式。这也无济于事 使用脚本使用的完整说明(可以在本文底部找到),我制作了一个函数,它生成一个image元
for ( var i = 0; i <= cloudNo/4; i ++ ) {
// call a function which creates a new div and chip object
var cloudName = "flyingCloud" + i.toString();
newCloud(i);
movechip(cloudName);
}
但我一直在犯这样的错误:
moveobj.js:71未捕获的TypeError:无法读取null(…)的属性“style”
这是问题代码的链接,第71行试图访问“芯片”的样式属性:
我意识到这可能是一个很容易解决的问题,但似乎无法找出问题所在。如果您能提供任何意见,我将不胜感激
可以在此处找到完整的说明:setAttribute仅将样式属性覆盖到最后一个!我不知道,所以我为你找到了另一个解决方案
setAttribute仅将样式属性覆盖到最后一个!我不知道,所以我为你找到了另一个解决方案
我认为你不能一次完成多个样式,一次尝试一个吗?我在控制台上检查过,它们的样式属性肯定在命名属性上(
CSSStyleDeclaration 0:“position”1:“left”2:“width”3:“height”4:“font size”
)。我更改并单独添加了它们,只是为了看看是否有任何区别,并且只成功附加了一个属性(最后一个)。document.getElementById(chip.named)
为空,因为movechip
中的芯片没有名为的属性,它只是一个常规div。由于脚本需要全局变量,因此可以从window[cloudName]=newchip(cloudName,362362)开始新云的最后一行中的code>。这将使您克服这个特定的错误,但老实说,这个脚本已经有十多年的历史了,按照今天的标准,它相当糟糕,因此我发现进一步的调试和修复是毫无意义的。飞行元素的这种效果完全不需要JavaScript就可以实现。我不认为你可以一次完成多个样式,一次尝试一个?我在控制台上检查过,它们的样式属性肯定在命名属性上(CSSStyleDeclaration 0:“position”1:“left”2:“width”3:“height”4:“font size”
)。我更改并单独添加了它们,只是为了看看是否有任何区别,并且只成功附加了一个属性(最后一个)。document.getElementById(chip.named)
为空,因为movechip
中的芯片没有名为的属性,它只是一个常规div。由于脚本需要全局变量,因此可以从window[cloudName]=newchip(cloudName,362362)开始新云的最后一行中的code>。这将使您克服这个特定的错误,但老实说,这个脚本已经有十多年的历史了,按照今天的标准,它相当糟糕,因此我发现进一步的调试和修复是毫无意义的。飞行元素的这种效果完全不需要JavaScript就可以实现。
// cloud function
function newCloud(number) {
// assign a 'name' to the cloud to be used as the id and then passed to the chip function
var cloudName = "flyingCloud" + number.toString();
// create a div element to house the image
var cloud = document.createElement('div');
// create an image element
var cloudImg = document.createElement('img');
// append the image to the div
cloud.appendChild(cloudImg);
// assign image src as cloud url
cloudImg.src = cloudImageUrl;
// assign the cloudname as the ID
cloud.id = cloudName;
// set the style of the cloud div
cloud.setAttribute('style', 'position:absolute; left: -500px; width:50; height:62; font-size:10px;');
// append the cloud to the container div
cloudDiv.appendChild(cloud);
// create a new chip
cloud = new Chip(cloudName, 362,362);
}
cloud.style.position = "absolute";
cloud.style.left = "-500px";
cloud.style.width = "50px";
cloud.style.height = "62px";
cloud.style.fontSize = "10px";