簡體   English   中英

構造新對象時未捕獲類型錯誤

[英]uncaught type error when constructing new object

我正在拼湊一些腳本來在我的頁面上創建浮雲。

  1. 我嘗試調用movechip()函數時出現問題。
  2. 在調用該函數之前(在for循環中或在構造雲的函數中)div和圖像被附加到容器div,並且對象包含正確的屬性(使用開發人員工具中的元素檢查器進行驗證)
  3. 我已經嘗試將movechip()調用從構造函數移動到for循環,但這沒有幫助
  4. 我刪除了原始樣式(使用css)並使用javascript附加了樣式。 這也沒有幫助。

使用腳本使用的完整說明(可以在本文的底部找到)我創建了一個函數,它生成一個圖像元素,將它附加到div,將div附加到頁面並調用“芯片”構造函數。 我在for循環中調用函數來獲取頁面上我想要的雲數。

這是for循環:

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);
}

這是功能:

    // 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);
}

但我一直收到這個錯誤:moveobj.js:71 Uncaught TypeError:無法讀取屬性'style'的null(...)

這里是問題代碼的鏈接,第71行試圖訪問'chip'的樣式屬性: http//samisite.com/extras/HTMLobj-1640/moveobj.js

我意識到這可能是一個非常容易解決的問題,但似乎無法弄清楚什么是錯的。 任何意見我都會非常感激。

完整的說明可以在這里找到: http//dynamicdrive.com/dynamicindex4/flyimage.htm

setAttribute只覆蓋style屬性到最后一個! 我不知道,所以我找到了另一個解決方案

樣式化JS Dom對象

cloud.style.position = "absolute";
cloud.style.left = "-500px";
cloud.style.width = "50px";
cloud.style.height = "62px";
cloud.style.fontSize = "10px";

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM