在加载和点击隐藏/显示div

时间:2016-02-11 13:15:52

标签: javascript jquery

我想要做的是在加载时隐藏#p0的所有段落。 另外,如果用户点击它的兄弟跨度,我想显示一个段落,并隐藏所有其他段落

<ul>
   <li>
      <h1>
        <span id="span0">Lorem</span>
        <p id="p0">Lorem ipsum dolor sit amet,</p>
      </h1>
   </li>
   <li>
      <h1>
        <span id="span1">Lorem2</span>
        <p id="p1">Lorem ipsum dolor sit amet,</p>
      </h1>
   </li>
   <li>
      <h1>
        <span id="span2">Lorem3</span>
        <p id="p2">Lorem ipsum dolor sit amet,</p>
      </h1> 
   </li>
...

3 个答案:

答案 0 :(得分:1)

$('p:not("#p0")').hide();

$('span').on('click',function() {
    $('p').hide();
    $(this).next('p').show();
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<ul>
   <li>
      <h1>
        <span id="span0">Lorem</span>
        <p id="p0">Lorem ipsum dolor sit amet,</p>
      </h1>
   </li>
   <li>
      <h1>
        <span id="span1">Lorem2</span>
        <p id="p1">Lorem ipsum dolor sit amet,</p>
      </h1>
   </li>
   <li>
      <h1>
        <span id="span2">Lorem3</span>
        <p id="p2">Lorem ipsum dolor sit amet,</p>
      </h1> 
   </li>
 </ul>

如果您希望将其与页面上的其他p / span隔离,则可以使用'prefixed-by' attribute selector (^=) ...

$('p[id^="p"]:not("#p0")').hide();

$('span[id^="span"]').on('click',function() {
    $('p[id^="p"]').hide();
    $(this).next('p[id^="p"]').show();
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<ul>
   <li>
      <h1>
        <span id="span0">Lorem</span>
        <p id="p0">Lorem ipsum dolor sit amet,</p>
      </h1>
   </li>
   <li>
      <h1>
        <span id="span1">Lorem2</span>
        <p id="p1">Lorem ipsum dolor sit amet,</p>
      </h1>
   </li>
   <li>
      <h1>
        <span id="span2">Lorem3</span>
        <p id="p2">Lorem ipsum dolor sit amet,</p>
      </h1> 
   </li>
 </ul>

答案 1 :(得分:0)

$('#p0').hide();

$('span').on('click',function() {
    $('p').hide();
    $(this).next('p').show();
});

答案 2 :(得分:0)

using System;
class TwoD
{
    static void Main()
    {
        int[][,] a = new int[3][,];
        a[0] = new int[2, 2];
    //    a[1] = new int[3, 3];
        int i,j;
        for (i = 0; i < 2; i++)
        {
            for (j = 0; j < 2; j++)
            {
                a[0][i,j] = i;//confused
            }
        }

        for (i = 0; i < 2; i++)
        {
            for (j = 0; j < 2; j++)
            {

                Console.WriteLine(a[0][i, j] + " "); 
                Console.WriteLine();
            }
        }