在没有href的情况下在div上使用target

时间:2015-11-17 20:58:20

标签: html css

我创建了一个id为“dashboard”的行div,其中我有3个div。在这行下面,我创建了3行,每行有一个div。我想点击ID为“dashboard”的行div中的一个div,然后打开id为“alarm”的行div。我想使用target来实现此任务,但不幸的是,target只能与href标签一起使用。这是我的代码

    @charset "utf-8";
/* CSS Document */

* {
    box-sizing: border-box;
}

.row:after {
    content: "";
    clear: both;
    display: block;
}

[class*="col-"] {
    float: left;
    padding: 15px;

}

.col-1 {width: 8.33%;}
.col-2 {width: 16.66%;}
.col-3 {width: 25%;}
.col-4 {width: 33.33%;}
.col-5 {width: 41.66%;}
.col-6 {width: 50%;}
.col-7 {width: 58.33%;}
.col-8 {width: 66.66%;}
.col-9 {width: 75%;}
.col-10 {width: 83.33%;}
.col-11 {width: 91.66%;}
.col-12 {width: 100%;}

.show{
    display: none;


    }

.show:target{
    display: block;
    }

这是它的css代码:

{{1}}

2 个答案:

答案 0 :(得分:5)

与此JS Fiddle一样,这是一个仅限CSS的解决方案,只需将您的div替换为a并使用:target即可你想要的方式:

   @charset"utf-8";

   /* CSS Document */
   * {
     box-sizing: border-box;
   }
   .row:after {
     content: "";
     clear: both;
     display: block;
   }
   [class*="col-"] {
     float: left;
     padding: 15px;
   }
   .col-1 {
     width: 8.33%;
   }
   .col-2 {
     width: 16.66%;
   }
   .col-3 {
     width: 25%;
   }
   .col-4 {
     width: 33.33%;
   }
   .col-5 {
     width: 41.66%;
   }
   .col-6 {
     width: 50%;
   }
   .col-7 {
     width: 58.33%;
   }
   .col-8 {
     width: 66.66%;
   }
   .col-9 {
     width: 75%;
   }
   .col-10 {
     width: 83.33%;
   }
   .col-11 {
     width: 91.66%;
   }
   .col-12 {
     width: 100%;
   }
   .show {
     display: none;
   }
   :target {
     display: block;
   }
<div id="dashboard" class="row">
  <a href="#alarm" class="col-4" style="background-color:#009"></a>
  <a href="#calendar" class="col-4" style="background-color:#C00"></a>
  <a href="#weather" class="col-4" style="background-color:#0C0"></a>
</div>
<div id="alarm" class="row show">
  <div class="col-12" style="background-color:#009"></div>
</div>
<div id="calendar" class="row show">
  <div class="col-12" style="background-color:#C00"></div>
</div>
<div id="weather" class="row show">
  <div class="col-12" style="background-color:#0C0"></div>
</div>

更新:根据评论中的请求,现在在每个<a href="#dashboard">小工具中添加.show并执行相同的:target招数得到这个结果JS Fiddle 2

   @charset"utf-8";

   /* CSS Document */
   * {
     box-sizing: border-box;
     margin: 0;
     padding: 0;
   }
   .row:after {
     content: "";
     clear: both;
     display: block;
   }
   [class*="col-"] {
     float: left;
     padding: 15px;
   }
   .col-1 {
     width: 8.33%;
   }
   .col-2 {
     width: 16.66%;
   }
   .col-3 {
     width: 25%;
   }
   .col-4 {
     width: 33.33%;
   }
   .col-5 {
     width: 41.66%;
   }
   .col-6 {
     width: 50%;
   }
   .col-7 {
     width: 58.33%;
   }
   .col-8 {
     width: 66.66%;
   }
   .col-9 {
     width: 75%;
   }
   .col-10 {
     width: 83.33%;
   }
   .col-11 {
     width: 91.66%;
   }
   .col-12 {
     width: 100%;
   }
   .show {
     display: none;
     position: absolute;
     width: 100%;
     height: 400px;
     top: 0;
   }
   :target {
     display: block;
   }
   .back-btn {
     width: 60px;
     height: 40px;
     display: inline-block;
     z-index: 20;
     background-color: orange;
     position: absolute;
     padding-top: 10px;
     text-align: center;
     text-decoration: none;
     text-transform: uppercase;
     color: black;
   }
<div id="dashboard" class="row">
  <a href="#alarm" class="col-4" style="background-color:#009"></a>
  <a href="#calendar" class="col-4" style="background-color:#C00"></a>
  <a href="#weather" class="col-4" style="background-color:#0C0"></a>

</div>
<div id="alarm" class="alarm row show">
  <a href="#dashboard" class="back-btn">back</a>
  <div class="col-12" style="background-color:#009; height:300px;"></div>
</div>
<div id="calendar" class="row show">
  <a href="#dashboard" class="back-btn">back</a>
  <div class="col-12" style="background-color:#C00; height:300px;"></div>
</div>
<div id="weather" class="row show">
  <a href="#dashboard" class="back-btn">back</a>
  <div class="col-12" style="background-color:#0C0; height:300px;"></div>
</div>

** 请注意,我已将height:300px;添加到.show DIV 的内联样式中。

答案 1 :(得分:0)

无论如何,目标并不是正确的方法。放一个&#34; onclick =&#34;在div上,并编写一个Javascript函数来处理行为。

    <div id="dashboard" class="row">

        <div  class="col-4" style="background-color:#009" onclick="myfunction('col4')">
        </div>
</div>

       function myfunction(whichColumn){
          //do some stuff with whichColumn
       }