JS寻找子父结点上的类型

老话。没有技术含量。只不过是作为储备而用。
<script language="javascript">
function ShowOrHideSpan(elmt)
{
    var srcElmt = event.srcElement;
    if ( srcElmt && srcElmt.tagName == 'TD' && srcElmt.name == 'title' )
    {
         var span = FindChildElement(elmt, 'SPAN');
         if ( span.style.display == 'none' )
         {
              span.style.display = 'inline';
         }
         else
         {
              span.style.display = 'none';
         }      
    }  
}

 

function FindParentElement(element, tagName)
{
    while(element != null && element.tagName != tagName )
    {
        element = element.parentElement;
    }
    if ( element != null && element.tagName == tagName )
    {
        return element;
    }
    return null;
}


function FindChildElement(element, tagName)
{
    var isFounded = false;
    var elements = element;
    var result = element;
    if ( element.tagName == tagName )
    {
        return element;
    }
    while(!isFounded && elements != null && result != null && result.tagName != tagName)
    {
        elements = elements.childNodes;
        for( var i=0 ; elements != null && i < elements.length ; i++ )
        {
            result = elements.item(i);
            var result2 = FindChildElement(result, tagName);
            if ( result == null || result2 == null )
            {
                continue;
            }
            if ( result.tagName == tagName || result2.tagName == tagName )
            {
                if ( result2.tagName == tagName )
                {
                    result = result2;
                }
                isFounded = true;
                break;
            }
        }
    }
    if ( isFounded )
    {
        return result;
    }
    else
    {
        return null;
    }
}

</script>

 

<table border="1" ondblclick="ShowOrHideSpan(this)" style="border-collapse: collapse;">
    <tr>
         <td name="title">
             Tilte</td>
    </tr>
    <tr>
         <td>
             <span>Content: asdf asdf asdf</span></td>
    </tr>
</table>

你可能感兴趣的:(js)