2012-08-07 66 views
0

所以,我有一些JavaScript代碼,我用Greasemonkey在本地測試。但是,我得到這個錯誤持續在Firefox的錯誤控制檯:Javascript錯誤:catChildNotes [y] .setAttribute不是函數

catChildNotes [Y] .setAttribute不是一個功能

代碼:

var i = prompt("How many videos have you got?", ""); 
function remove_mp4() 
{ 
    titleElems=document.getElementsByName("title"); 
    for(i=0; i<titleElems.length; i++) 
    { 
     titleInner=titleElems[i].innerHTML; 
     titleElems[i].innerHTML=titleInner.replace(titleInner.match(".mp4"), ""); 
    } 
} 
for (var x = 0; x < i; i++) 
{ 
    document.getElementsByName("description")[x].value = "Visit me on my web-site :\ 
    \ 
                  http://www.sample.com/"; 
    document.getElementsByName("keywords")[x].value = prompt("Enter keywords : ",""); 
    catChildNodes=document.getElementsByName("category")[x].childNodes; 
    catChildNodes[x + 1].removeAttribute("selected"); 
    for(y=0; y<catChildNodes.length; y++) 
{ 
     if(catChildNodes[y].value="27") 
    { 
      catChildNodes[y].setAttribute("selected",""); 
    } 
} 
} 
remove_mp4(); 

該腳本應該在YouTube上運行上傳頁面並執行以下操作:

  • 從標題中刪除「.mp4」
  • 添加默認說明
  • 添加關鍵字(這等於提示值)
  • 更改類別爲「教育」
+0

的機會是'catChildNodes [Y]'是不是你認爲它是和物業'setAttribute'被評估爲'undefined'。 。那麼* catChildNodes [y]'會評估什麼?爲什麼? – 2012-08-07 23:03:05

+0

你是什麼意思?你能解釋一下好嗎? – TheGhost 2012-08-07 23:05:27

+0

它評估在網頁的HTML元素中命名proprety。 Name屬性被設置爲「category」,所以它找到它並得到它的值。它的索引是var x,因爲x越來越多,所以如果我有10個視頻,x會先是0,然後是9,將我的所有視頻類別更改爲「Education」。希望我解釋得很好! :) – TheGhost 2012-08-07 23:09:00

回答

2

通常,當你得到一個元素的子節點上,您會得到其他element nodestext nodes。前者有setAttribute方法,後者沒有(僅僅因爲文本節點沒有任何屬性)。如果您只需要元素子元素並且不需要文本節點,那麼您應該使用children property而不是childNodes

。在你的代碼中的至少一個以上的錯誤,這是不是一個比較:

if (catChildNodes[y].value = "27") 

這將分配值27 catChildNodes[y].value。如果你真的想比較,那麼你應該使用比較運算符:

if (catChildNodes[y].value == "27")