最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to show and hide a simple <ol> list with onclick? - Stack Overflow

programmeradmin8浏览0评论

Considering the following paragraph and list:

<p id = "list1" onclick = "openList1()">List of Items</p>
<ol>
  <li><a href = "/exampleFolder/file1.txt">List Item 1</a></li>
  <li><a href = "/exampleFolder/file2.txt">List Item 2</a></li>
  <li><a href = "/exampleFolder/file3.txt">List Item 3</a></li>
  <li><a href = "/exampleFolder/file4.txt">List Item 4</a></li>
  <li><a href = "/exampleFolder/file5.txt">List Item 5</a></li>
</ol>

How can I show and hide this entire list with Javascript?

<script>
function openList1() {
...
}
</script>

I thank you for the attention!

Considering the following paragraph and list:

<p id = "list1" onclick = "openList1()">List of Items</p>
<ol>
  <li><a href = "/exampleFolder/file1.txt">List Item 1</a></li>
  <li><a href = "/exampleFolder/file2.txt">List Item 2</a></li>
  <li><a href = "/exampleFolder/file3.txt">List Item 3</a></li>
  <li><a href = "/exampleFolder/file4.txt">List Item 4</a></li>
  <li><a href = "/exampleFolder/file5.txt">List Item 5</a></li>
</ol>

How can I show and hide this entire list with Javascript?

<script>
function openList1() {
...
}
</script>

I thank you for the attention!

Share Improve this question asked Jul 10, 2013 at 1:34 Ericson WilliansEricson Willians 7,84515 gold badges67 silver badges124 bronze badges 1
  • 1 Google "show and hide a div" and you'll see lots of ways. – Terry Commented Jul 10, 2013 at 1:37
Add a ment  | 

5 Answers 5

Reset to default 6

You can give an id to the OL list.

<p id = "list1" onclick = "openList1()">List of Items</p>
<ol id="ollist">
  <li><a href = "/exampleFolder/file1.txt">List Item 1</a></li>
  <li><a href = "/exampleFolder/file2.txt">List Item 2</a></li>
  <li><a href = "/exampleFolder/file3.txt">List Item 3</a></li>
  <li><a href = "/exampleFolder/file4.txt">List Item 4</a></li>
  <li><a href = "/exampleFolder/file5.txt">List Item 5</a></li>
</ol>

And then in your javascript you can toggle it like this...

<script>
function openList1() {
    var list = document.getElementById("ollist");

    if (list.style.display == "none"){
        list.style.display = "block";
    }else{
        list.style.display = "none";
    }
}
</script>
var myList = document.getElementsByTagName('ol');
myList[0].style.visibility = 'hidden'; // 'visible'
<script>
function openList1() {
$("ol").toggle();
}
</script>

Can you use JQuery? If so, try the above

First you can modify your list:

<ol id="list" style="display: none;">

You can write a function to show:

function showStuff(id) {
    document.getElementById(id).style.display = 'block';
}
// call function to show your list
showStuff("list");

To hide your element:

function hideStuff(id) {
    document.getElementById(id).style.display = 'none';
}
// call function to hide your list
hideStuff("list");
var ol = document.getElementByTagName('ol');
 ol.style.display='none';
发布评论

评论列表(0)

  1. 暂无评论