日期:2014-05-17  浏览次数:20535 次

javascript代码兼容问题,IE9,火狐正常,IE8下出现错误! 求助啊大神!!!!!!!!
js代码如下
JScript code

 <script type="text/javascript">
        function AddFile() {
            if (i <= 10) {
                document.getElementById("divPanel").innerHTML += "<div>照片" + i + ":<input type='file' size='30' name='File'/></div>";
                i++;
            }
        }
        function RemoveFile() {
            var text = document.getElementById("divPanel").innerHTML;
            document.getElementById("divPanel").innerHTML = text.substr(0, text.lastIndexOf("<div>"));
            if (i >= 2) {
                i--;
            }
            else {
                i = 1;
            }
        }
        var i = 2;
    </script>




前台代码:
 
HTML code
<table border="0" width="100%" style="position: relative;">
                <tr>
                    <td valign="top" width="100px" align="center">
                        <div>
                            <span style="color: #8600FF;" onclick="AddFile()" onmousemove="javascript:this.style.cursor='pointer';">
                                <img src="Images/OtherImages/more.jpg" />添加</span>
                        </div>
                        <div>
                            <span style="color: #8600FF;" onclick="RemoveFile()" onmousemove="javascript:this.style.cursor='pointer';">
                                <img src="Images/OtherImages/jian.jpg" />移除</span>
                        </div>
                    </td>
                    <td>
                        <div id="divPanel">
                            <div>
                                照片1:<input type='file' size="30" name='File' /></div>
                        </div>
                    </td>
                </tr>
                <tr>
                    <td>
                    </td>
                    <td>
                        <asp:ImageButton ID="ImgBtnUpLoad" ImageUrl="~/Images/OtherImages/UpLoadImage.jpg"
                            runat="server" OnClick="ImgBtnUpLoad_Click" />
                    </td>
                </tr>
            </table>


在IE8下,点击移除按钮,DivPanel里的File控件全部会被移除掉 ,没有道理啊,是IE8不支持substr函数还是用substr函数得到的结果跟IE9,火狐不一样? 求大神支招!

------解决方案--------------------
帮你改了下,ie ff都可以
HTML code
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>无标题页</title>

    <script type="text/javascript">
        function AddFile() {
            if (i <= 10) {
                var div = document.createElement("div");
                div.innerHTML = "照片" + i + ":<input type='file' size='30' name='File'/>";
                document.getElementById("divPanel").appendChild(div);
                i++;
            }
        }
        function RemoveFile() {
            var text = document.getElementById("divPanel");
            var divCollection = text.getElementsByTagName("div");
            text.removeChild(divCollection[divCollection.length-1])
            if (i >= 2) {
                i--;
            }
            else {