2016-04-26 34 views
-1

我有一個函數來實際創建一個HTML元素並快速綁定它。爲什麼如果我的函數中的作用域被JavaScript解釋器完全忽略了?

然而,它不工作,因爲它應該。

當我使用調試(ggCreateElement)時,我發現即使語句爲「true」,它也會完全跳過if範圍。我已經用完了想法。

function ggCreateElement(tagName,className,idName,appendPointTagName) { 
    d=document.createElement(tagName); 
    d.className=className; 
    d.id=idName; 
    ap=document.getElementsByTagName(appendPointTagName); 
     if (ap.lenght==0) { 
      console.log("Append point tag name is not found ! ") ; 
     } 
     else { 
      ap[0].appendChild(d); 
      return d; 
    } 
} 

請問您能幫我嗎?謝謝。

+0

如何你調用它嗎? – Rayon

+3

'ap.lenght'?你正在粘貼正確的代碼嗎?也''console.log(ap.length);'在if塊之前 –

+0

調試時'ap.lenght'的值是多少? – Paddy

回答

2

的代碼工作正常,並返回一旦你如果條件從

if (ap.lenght==0) 

改變你的錯字到

if (ap.length==0) 

的方法,另外,你不妨做適當的元素

if (!ap.length) 
相關問題