为什么我不能改变标签的innerHTML内容?

时间:2023-01-17 18:54:02
有如下的HTML代码:
  <table id ='mainTable' width="100%" border="0" cellpadding="0" cellspacing="1"  class="List">
                   <!---&Aacute;&ETH;&Atilde;&ucirc;--->

     <tr> 
                        <td  class="List_item"><input type="checkbox" name="checkbox" value="checkbox"></td>
                        <td  class="List_item">YH_DM</td>
                        <td  class="List_item">YHZL_DM</td>
                        <td   class="List_item">YH_MC</td>
                        <td   class="List_item">YH_JC</td>
                        <td   class="List_item">SWJGDM</td>
                        <td   class="List_item">XYBZ</td>
                      </tr>
  
    

<tr class="List_tr"> 
                        <td   ><input type="checkbox" name="checkbox2" value="checkbox"></td>
                        <td   >&33;</td>
                        <td >&33;</td>
                        <td  >&33;</td>
                        <td  >&33;</td>
                        <td  >&222;</td>
                        <td  >&22;</td>
                      </tr>
                      <tr class="List_tr"> 
                        <td  ><input type="checkbox" name="checkbox3" value="checkbox"></td>
                        <td  >&nbsp;</td>
                        <td >&nbsp;</td>
                        <td >&nbsp;</td>
                        <td >&nbsp;</td>
                        <td >&nbsp;</td>
                        <td >&nbsp;</td>
                      </tr>
  </table>
<input type='button' name='change' value='change' onclick='change()'>
<script>
function change(){
document.getElementById("mainTable").innerHTML="";

}
</script>

点击按钮后,IE报错:未知的运行时错误

11 个解决方案

#1


<table id ='mainTable' width="100%" border="0" cellpadding="0" cellspacing="1"  class="List">
                   <!---&Aacute;&ETH;&Atilde;&ucirc;--->

     <tr>
                        <td  class="List_item"><input type="checkbox" name="checkbox" value="checkbox"></td>
                        <td  class="List_item">YH_DM</td>
                        <td  class="List_item">YHZL_DM</td>
                        <td   class="List_item">YH_MC</td>
                        <td   class="List_item">YH_JC</td>
                        <td   class="List_item">SWJGDM</td>
                        <td   class="List_item">XYBZ</td>
                      </tr>



<tr class="List_tr">
                        <td   ><input type="checkbox" name="checkbox2" value="checkbox"></td>
                        <td   >&33;</td>
                        <td >&33;</td>
                        <td  >&33;</td>
                        <td  >&33;</td>
                        <td  >&222;</td>
                        <td  >&22;</td>
                      </tr>
                      <tr class="List_tr">
                        <td  ><input type="checkbox" name="checkbox3" value="checkbox"></td>
                        <td  >&nbsp;</td>
                        <td >&nbsp;</td>
                        <td >&nbsp;</td>
                        <td >&nbsp;</td>
                        <td >&nbsp;</td>
                        <td >&nbsp;</td>
                      </tr>
  </table>
<input type='button' name='change' value='change' onclick='change()'>
<script>
function change(){
document.getElementById("mainTable").deleteRow();
document.getElementById("mainTable").deleteRow();
document.getElementById("mainTable").deleteRow();

}
</script>

#2


function change(){
document.getElementById("mainTable").innerText="" ;

}

#3


谢谢你们的回答,你们回答的是正确。为什么用innerHTML不行呢?

#4


不知道了,不过outerHTML可以用

#5


innerHTML应用与<div></div><span></span>这种中间没有别的元素的标签,table中间有tr和td则不行

#6


function change(){
document.getElementById("mainTable").outerHTML="" ;

}

#7


innerHTML对于table,tr等标签是只读的。

#8



只为什么table的innerText却可以改变呢?
在DOM模型中,innerText不是也是只读的吗?
我查看了一些文档,文档里写了innerText:
The innerText property is read-only on the HTML, TABLE, TBODY, TFOOT, THEAD, and TR objects.

When the innerText property is set, the given string completely replaces the existing content of the object.

#9


理论上应该是只读的,至于table标签为什么可用,我也不清楚,

#10


MSDN如是说:
The property is read/write for all objects except the following, for which it is read-only: COL, COLGROUP, FRAMESET, HTML, STYLE, TABLE, TBODY, TFOOT, THEAD, TITLE, TR. The property has no default value.

#11


哎,不知道哪个是真哪个是假?

#1


<table id ='mainTable' width="100%" border="0" cellpadding="0" cellspacing="1"  class="List">
                   <!---&Aacute;&ETH;&Atilde;&ucirc;--->

     <tr>
                        <td  class="List_item"><input type="checkbox" name="checkbox" value="checkbox"></td>
                        <td  class="List_item">YH_DM</td>
                        <td  class="List_item">YHZL_DM</td>
                        <td   class="List_item">YH_MC</td>
                        <td   class="List_item">YH_JC</td>
                        <td   class="List_item">SWJGDM</td>
                        <td   class="List_item">XYBZ</td>
                      </tr>



<tr class="List_tr">
                        <td   ><input type="checkbox" name="checkbox2" value="checkbox"></td>
                        <td   >&33;</td>
                        <td >&33;</td>
                        <td  >&33;</td>
                        <td  >&33;</td>
                        <td  >&222;</td>
                        <td  >&22;</td>
                      </tr>
                      <tr class="List_tr">
                        <td  ><input type="checkbox" name="checkbox3" value="checkbox"></td>
                        <td  >&nbsp;</td>
                        <td >&nbsp;</td>
                        <td >&nbsp;</td>
                        <td >&nbsp;</td>
                        <td >&nbsp;</td>
                        <td >&nbsp;</td>
                      </tr>
  </table>
<input type='button' name='change' value='change' onclick='change()'>
<script>
function change(){
document.getElementById("mainTable").deleteRow();
document.getElementById("mainTable").deleteRow();
document.getElementById("mainTable").deleteRow();

}
</script>

#2


function change(){
document.getElementById("mainTable").innerText="" ;

}

#3


谢谢你们的回答,你们回答的是正确。为什么用innerHTML不行呢?

#4


不知道了,不过outerHTML可以用

#5


innerHTML应用与<div></div><span></span>这种中间没有别的元素的标签,table中间有tr和td则不行

#6


function change(){
document.getElementById("mainTable").outerHTML="" ;

}

#7


innerHTML对于table,tr等标签是只读的。

#8



只为什么table的innerText却可以改变呢?
在DOM模型中,innerText不是也是只读的吗?
我查看了一些文档,文档里写了innerText:
The innerText property is read-only on the HTML, TABLE, TBODY, TFOOT, THEAD, and TR objects.

When the innerText property is set, the given string completely replaces the existing content of the object.

#9


理论上应该是只读的,至于table标签为什么可用,我也不清楚,

#10


MSDN如是说:
The property is read/write for all objects except the following, for which it is read-only: COL, COLGROUP, FRAMESET, HTML, STYLE, TABLE, TBODY, TFOOT, THEAD, TITLE, TR. The property has no default value.

#11


哎,不知道哪个是真哪个是假?