當頁面還沒載入完的時候呼叫下面語句,會取不到「add」 這個對象,提示為空或不是對象 複製程式碼 程式碼如下: document.getElementById("add").onclick = function(){ alert("hello"); } 當使用便可取的物件複製程式碼 程式碼如下: 程式碼如下: window onload = function(){ document.getElementById("add").onclick = function(){ alert("hello"); } } 複製程式碼 程式碼如下: <BR>/ /選取的從左邊移到右邊<BR>function toright() { <BR>var firstElement = document.getElementById("first"); <BR>var secondElement = document.getElementById("second"); <BR>var <BR>var firstoptionElement = firstElement.getElementsByTagName("option"); <BR>var len = firstoptionElement.length; <BR>for(var i=0;i<len;i ){ <BR>if(firstElement.selectedIndex != -11 ){ //selectedIndex 是select 的屬性<BR>secondElement.appendChild(firstoptionElement[firstElement.selectedIndex]); <BR>} <BR>} <BR>} <BR>//全部移動到右邊(){ <BR>var firstElement = document.getElementById("first"); <BR>var secondElement = document.getElementById("second"); <BR>var firstoptionElement = firstElement.getElementsByTagName("option" >var len = firstoptionElement.length; <BR>for(var i=0;i<len;i ){ <BR>secondElement.appendChild(firstoptionElement[0]);//option選項選取時候索引為0 <BR> } <BR>} <BR>//雙擊移到右邊<BR>function db(){ <BR>/* //方法一<BR>var firstElement = document.getElementById("first"); <BR>var secondElement = document.getElementById("second"); <BR>var firstoptionElement = firstElement.getElementsByTagName("option"); <BR>var len = firstoptionElement.length; <BR>for(var i=0i<len; i ){ <BR>if(firstElement.selectedIndex != -1){ //selectedIndex 是select 的屬性<BR>secondElement.appendChild(firstoptionElement[firstElement.selectedIndex]); <BR>} <BR>} </ 🎜>//方法二<br>var firstElement = document.getElementById("first"); <br>var secondElement = document.getElementById("second"); <br>secondElement.Divendld(firstElementlectbendld(firstElement)s. ; <br>} <br><br> <BR><BR> 選項1 選項2 選項3 選項4 選項5 選項6 選項8