2016-11-24 27 views
1

我正在一起編寫一些腳本以在我的頁面上創建浮動雲。構建新對象時出現未捕獲類型錯誤

  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遺漏的類型錯誤:無法讀取屬性「風格'的空(...)

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

我意識到這可能是一個很容易解決的問題,但似乎無法弄清楚什麼是錯的。我會很感激任何輸入。

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

+0

我不認爲你可以一氣呵成做多的風格,嘗試一次一個的另一個解決方案? –

+0

我檢查了控制檯,他們的風格屬性肯定是在命名屬性(CSSStyleDeclaration 0:「position」1:「left」2:「width」3:「height」4:「font-size」')。我改變了他們單獨添加他們只是爲了看看是否有任何區別,只有一個屬性附加成功(最後一個)。 –

+0

'document.getElementById(chip.named)'爲null,因爲'movechip'裏面的'chip'沒有'named'屬性,它只是一個普通的div。 – pawel

回答

0

的setAttribute僅覆蓋style屬性到最後一個!我不知道,所以我找到您

Styling JS Dom object

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