Как изменить этот код JavaScript, чтобы расширить только скрытый текст - PullRequest
0 голосов
/ 31 июля 2010

Я хочу развернуть скрытый текст на веб-странице, и после некоторого (повторного) поиска в 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, поэтому должно было прийти такое глупое сомнение

Спасибо:)

1 Ответ

1 голос
/ 31 июля 2010

Просто измените свой код на

<!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>
...