开发者

Javascript - innerHTML function in IE

I have a html file with content

<html>
<body>
<script language="JavaScript">
    function fun1()
    {
        alert(document.getElementById('id1').innerHTML);
    }
</script>
<div id="id1">
    <ul>
        <li>Here111</li>
        <li>Here222</li>
        <li>Here333</li>
    </ul>
</div>
<a href="javascript:void(0)" onclick="fun1()">Click&开发者_如何学编程lt;/a>
</body>
</html>

In innerHTML function iam getting only

<ul>
    <li>Here111
    <li>Here222
    <li>Here333</li>
</ul>

The </li> tag is missing how can I get entire content?


You have set id of div as a "test" but you are getting it by "test1"

alert(document.getElementById('test1').innerHTML);

it should be

alert(document.getElementById("test").innerHTML);


Your script refers to an id called test1 but your html hasn't got any.

If you do it like this:

<html>
    <head>
        <script>
            function test(){
                alert(document.getElementById('test1').innerHTML);
            }
        </script>
    </head>
    <body>
        <div id="test1">
            <ul>
                <li>This is first</li>
                <li>This is Second</li>
            </ul>
        </div>
        <a href="#" onclick="test();">hhhhhh</a>
    </body>
</html>

it works fine even in IE 7/8.


Add <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> in the beginning of html


There also is outerHTML property (unfortunately, unsupported in some browsers). It returns the whole of element.


This is not incorrect. For <li> elements, the closing tag is optional in HTML so IE is returning a valid fragment of HTML here (not that it always does).

If you need closing tags, you'll need to do create your own HTML string by traversing the DOM.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜