如何修改此javascript代码以仅展开隐藏文本

时间:2010-07-31 11:26:36

标签: javascript text expand

我想在网页上展开隐藏文字,在google上进行一些(重新)搜索后,我遇到了以下代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<title>Test</title>


<script type="text/javascript">
function Expand(node)
{
  // Change the image (if there is an image)
  if (node.childNodes.length > 0)
  {
    if (node.firstChild.tagName == "IMG")
    {
      node.firstChild.src = "minus.gif";
    }
  }
  node.nextSibling.style.display = '';
}

function Collapse(node)
{
  // Change the image (if there is an image)
  if (node.childNodes.length > 0)
  {
    if (node.firstChild.tagName == "IMG")
    {
      node.firstChild.src = "plus.gif";
    }
  }
 node.nextSibling.style.display = 'none';
}

function Toggle(node)
{
  // Unfold the branch if it isn't visible
  if (node.nextSibling.style.display == 'none')
  {        
      Expand(node);
  }
  // Collapse the branch if it IS visible
  else
  {        
     Collapse(node);
  }
node.childNodes[1].nodeValue = (node.nextSibling.style.display == 'none')? 'More...' : 'Less...';
}           
</script> 

</head>


<body>


<a onclick="Toggle(this)" style="cursor:pointer;"><img src="plus.gif" alt="Expand/Collapse" /> More...</a><p style="display:none;">This is a sample of the expanded text that will show up when "+ More..." is clicked</p>              


</body>
</html>

现在,脚本会在其旁边显示带有“更多...”的加号(展开)的.gif图像,当点击.gif文件或“更多...”时,隐藏文字会出现并且加号.gif被minus.gif替换,'More ...'更改为'Less ...' 但我想以另一种方式实现它,我希望一旦点击展开(plus.gif),再没有其他.gif应该出现,我不知道如何修改代码来做到这一点,所以朋友们请帮助我

我是javascript的新手,所以这样一个愚蠢的怀疑不得不来了

Thanx:)

1 个答案:

答案 0 :(得分:1)

只需将您的代码更改为此

即可
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Test</title>
        <script type="text/javascript">

            function ExpandDelete(node)
            {
                if (node.nextSibling.style.display == 'none')
                {        
                    node.nextSibling.style.display = '';
                    node.parentNode.removeChild(node);
                }
            }           
        </script> 
    </head>

    <body>
        <a onclick="ExpandDelete(this);" style="cursor:pointer;"><img src="plus.gif" alt="Expand" /> More...</a><p style="display:none;">This is a sample of the expanded text that will show up when "+ More..." is clicked</p>              
    </body>
</html>