点击实现折叠展开,在IE里不行,在chrome里没问题,为什么?

浏览:914 发布日期:2014/02/14 分类:求助交流 关键字: IE和CHROME 兼容性问题
我写的一个点击链接实现折叠、展开功能的源码。放在某html里(如D:\1.html),用IE没问题,用chrome则只能展开、不能折叠。如果将源码放到thinkphp源码里,原样粘贴到某文件中(如user\index.html),则在chrome里没问题,在IE里则完全没法用。
诡异问题,请教大虾。<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>test</title>
</head>

<body>
<script language="javascript" type="text/javascript">
document.getElementsByClassName = function(cl) 
{
    var retnode = [];
    var myclass = new RegExp('\\b'+cl+'\\b');
    var elem = this.getElementsByTagName('*');
    for (var j = 0; j < elem.length; j++) {
        var classes = elem[j].className;
        if (myclass.test(classes)) retnode.push(elem[j]);
    }
    return retnode;
}
function setCookie(sName,sValue) 
{
    var cookieString = sName + "=" + escape(sValue);
    document.cookie = cookieString;
}

function getCookie(sName) 
{
    var aCookie = document.cookie.split("; ");
    for (var j=0; j < aCookie.length; j++){
        var aCrumb = aCookie[j].split("=");
        if (escape(sName) == aCrumb[0])
            return unescape(aCrumb[1]);
    }
    return null;
}

function foo(idx)
{
    var items = document.getElementsByClassName(idx);
    var miniPic = document.getElementById(idx);
    
    for (var j=0; j<items.length; j++) 
    {
        if (getCookie(items[j].getAttribute("id")) == null)
        {
            miniPic.setAttribute('src', 'jian.png');
            items[j].setAttribute('style', 'display:');
            setCookie(items[j].getAttribute("id"), "1");
        }
        else
        {
            if (getCookie(items[j].getAttribute("id")) == 1) {
                miniPic.setAttribute('src', 'jia.png');
                items[j].setAttribute('style', 'display:none;');
                setCookie(items[j].getAttribute("id"), "0");
            } else if (getCookie(items[j].getAttribute("id")) == 0) {
                miniPic.setAttribute('src', 'jian.png');
                items[j].setAttribute('style', 'display:');
                setCookie(items[j].getAttribute("id"), "1");
            } else {
                alert("error");
            }
        }
    }
}
</script>

    <table  border="1">
      <thead>
        <tr>
            <th>A</th>
            <th>B</th>
            <th>C</th>
            <th>D</th>
        </tr>
      </thead>
      <tfoot>
            <tr><th></th></tr>
      </tfoot>
        <tbody>
            <tr>
                <td><a href="javascript:void(0)"><img id="123456" onclick="foo(this.id)" src="jia.png" /></a></td>
                <td>totalName</td>
                <td>II</td>
                <td>sum</td>
            </tr>
            <tr class="123456" id="123" style='display:none'>
                <td></td>
                <td>child1</td>
                <td>II</td>
                <td>200</td>
            </tr>
            <tr class="123456" id="124" style='display:none'>
                <td></td>
                <td>child2</td>
                <td>II</td>
                <td>300</td>
            </tr>
        </tbody>
    </table>
</body>
</html>

附件 截图00.jpg ( 15.02 KB 下载:14 次 )

最佳答案
评论( 相关
后面还有条评论,点击查看>>