日期:2014-05-16  浏览次数:20401 次

急啊!!!关于TD不响应onmousedown简单问题,一经测试成功,马上揭帖
js代码

JScript code


function test()
{
    var myDocument = document;
    var htmlElement = myDocument.documentElement
    var bodyElement = htmlElement.getElementsByTagName("body")[0];
    var items = bodyElement.getElementsByTagName("a");
    for ( var i = 0 ; i < items.length; i ++ )
    {
        var itemAttributes = items[i].getAttribute("id");
        if ( itemAttributes.substr(0,3) == "aaa" )
        {
            var parentTD = items[i].parentNode ;
            if ( parentTD != null)
            {
                parentTD.setAttribute("height","500");
                alert(parentTD.getAttribute("onmousedown"));
            
                parentTD.setAttribute("onmousedown","function anonymous() { ShowDiv(); }");
                alert(parentTD.getAttribute("onmousedown"));
            } 
        }
    }
}
    
function ShowDiv()
{
        if ( window.event.button == "2")
    {
        alert("I'm a Event.....");
    }
}




HTML code

<body>
<table border = "1">
        <tr>
            <td> 
                <a id = "aaa" href="#">bbbb</a>
                <input type="button" value="test" onClick="test();"/>
            </td>
        </tr>
    </table>
</body>



js和html代码如上
为什么我在js代码中给td加的onmousedown事件总是不管用呢?



------解决方案--------------------
试一下这样

document.body.onmousedown=function()
{
if(event.srcElement.id.substring(0,3)=="aaa")
{
ShowDiv()
return false
}

}

function ShowDiv()
{
if ( window.event.button == "2")
{
alert("I'm a Event.....");
}
}


------解决方案--------------------
if ( window.event.button == "2")

改为

if ( window.event.button == 2)
------解决方案--------------------
parentTD.setAttribute("onmousedown","function anonymous() { ShowDiv(); }");


==>>>parentTD.attachEvent("onmousedown",ShowDiv);