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

js怎样获取父类div的id
有一个div,动态用js给div添加了一张表,现在点击table中的某一个td时,想要把这个div的id读出来~
请问有什么方法可以实现的?
HTML code

<div id="faceContainer" style='position: absolute;height: 150px;width: 300px;left: 27px;top: 100px;display:none;border: 1px solid #999999;'></div>


function loadFace()
{
    face = document.getElementById("faceContainer");
    var strTable = "<table border= \"1\" cellpadding=\"0\" cellspacing=\"0\">";
    var i,j,count;
    count = 0;
    for(i=0;i<7;i++)
    {
        strTable += "<tr>";
        for(j=0;j<14;j++)
        {
            if(count <=95)
            {
            
             strTable += "<td><img src='Face/" + count + ".gif' onclick=\"insertFace('"+count+"')\" alt=\"表情"+count+"\"/></td>";
            }
            else
            {
                strTable += "<td>&nbsp;</td>";
            }
            count++;
        }
        strTable += "</tr>";
    }
    strTable += "</table>";
    face.innerHTML = strTable;
}




在线等高手指点`

------解决方案--------------------
HTML code

<div id="faceContainer" style='position: absolute;height: 150px;width: 300px;left: 27px;top: 100px;display:none;border: 1px solid #999999;'></div>


function loadFace()
{
    face = document.getElementById("faceContainer");
    [color=#FF0000]var tbID="myTable";[/color]
    var strTable = "<table id=\""+myTable+"\" border= \"1\" cellpadding=\"0\" cellspacing=\"0\">";
    var i,j,count;
    count = 0;
    for(i=0;i<7;i++)
    {
        strTable += "<tr>";
        for(j=0;j<14;j++)
        {
            if(count <=95)
            {
            
             [color=#FF0000]strTable += "<td><img src='Face/" + count + ".gif' onclick=\"insertFace('"+count+"','"+tbID+"')\" alt=\"表情"+count+"\"/></td>";[/color]            }
            else
            {
                strTable += "<td>&nbsp;</td>";
            }
            count++;
        }
        strTable += "</tr>";
    }
    strTable += "</table>";
    face.innerHTML = strTable;
}

------解决方案--------------------
HTML code
<div id="faceContainer" style='position: absolute;height: 150px;width: 300px;left: 27px;top: 100px;display:block;border: 1px solid #999999;'></div>

<script>
window.onload=function(){loadFace();};
function loadFace()
{
    var face = document.getElementById("faceContainer");
    var strTable = "<table border= '1' cellpadding='0' cellspacing='0'>";
    var i,j,count;
    count = 0;
    for(i=0;i<7;i++)
    {
        strTable += "<tr>";
        for(j=0;j<14;j++)
        {
            if(count <=95)
            {
            
             strTable += "<td><img src='Face/" + count + ".gif' onclick='insertFace("+face.id+")' alt='表情"+count+"'/></td>";
            }
            else
            {
                strTable += "<td>&nbsp;</td>";
            }
            count++;
        }
        strTable += "</tr>";
    }
    strTable += "</table>";
    face.innerHTML = strTable;
}
function insertFace(face){
    alert(face.id);
}
</script>

------解决方案--------------------