当页面还没有加载完的时候调用下面语句,会取不到“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 firstoptionElement = firstElement.getElementsByTagName("option"); <BR>var len = firstoptionElement.length; <BR>for(var i=0;i<len;i++){ <BR>if(firstElement.selectedIndex != -1){ //selectedIndex 是select 的属性 <BR>secondElement.appendChild(firstoptionElement[firstElement.selectedIndex]); <BR>} <BR>} <BR>} <BR>//全部移动到右边 <BR>function allright(){ <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=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=0;i<len;i++){ <BR>if(firstElement.selectedIndex != -1){ //selectedIndex 是select 的属性 <BR>secondElement.appendChild(firstoptionElement[firstElement.selectedIndex]); <BR>} <BR>} */ <BR>//方法二 <BR>var firstElement = document.getElementById("first"); <BR>var secondElement = document.getElementById("second"); <BR>secondElement.appendChild(firstElement[firstElement.selectedIndex]); <BR>} <br><br> <br><br> 选项1 选项2 选项3 选项4 选项5 选项6 选项8