如何自定义排列位置绝对div标签

时间:2018-05-18 07:23:31

标签: html css position absolute

此处我在点击相应的项目时尝试显示div#content_1 , div#content_2 and div#content_3。我想在最上面点击最后点击的项目。如果您点击item3,那么您将无法查看item1和item2永远如果你点击item2一旦你看不到item1 forever.please帮助我解决这个问题,提前谢谢。(考虑我们有大量的项目)



function getElement(e) {
   var elem = document.getElementById(e);
   return elem;
}

getElement("item_1").addEventListener("click",function(){
    getElement("content_1").style.display = "block";});
    
getElement("item_2").addEventListener("click",function(){
    getElement("content_2").style.display = "block";});
    
getElement("item_3").addEventListener("click",function(){
    getElement("content_3").style.display = "block";});

ul,li{
  padding:0px;
  margin:0px;
  list-style:none;
 }
 div#menu{
  width:100%;
  height:50px;
 }
 div#menu>ul li{
  float:left;
  border:1px solid #000;
  padding: 2px;
  background-color:blue;
  color:#fff;
  margin:5px;
  cursor:pointer;
 }
  
 #content{
  width:100%;
  border:1px solid #000;
  height:200px;
  position :relative;
 }
 
 .abs{
  position:absolute;
  width:100px;
  height:100px;
  border:1px solid #000;
  display:none;
 }
 
 #content_1{
  background-color:red;
 }
 #content_2{
  background-color:green;
 }
 #content_3{
  background-color:yellow;
 }

<div id=menu>
  <ul>
    <li id="item_1">menu item1</li>
    <li id="item_2">menu item2</li>
    <li id="item_3">menu item3</li>
  </ul>
</div>
<div id="content">
  <div id="content_1" class="abs"></div>
  <div id="content_2" class="abs"></div>
  <div id="content_3" class="abs"></div>
</div>
&#13;
&#13;
&#13;

4 个答案:

答案 0 :(得分:1)

希望这会对你有所帮助。如果您不想隐藏div,那么只需使用z-index。

function getElement(e) {
   var elem = document.getElementById(e);
   return elem;
}

function zIndexZero() {
document.querySelectorAll('.abs').forEach(function(item) {
  item.style.zIndex = 0;
});
}

getElement("item_1").addEventListener("click",function(){

    zIndexZero();
    getElement("content_1").style.display = "block";
    getElement("content_1").style.zIndex = 99;
    });
    
getElement("item_2").addEventListener("click",function(){
    zIndexZero();
    getElement("content_2").style.display = "block";
    getElement("content_2").style.zIndex = 99;
    
    });
    
getElement("item_3").addEventListener("click",function(){
    zIndexZero();
    getElement("content_3").style.display = "block";
    getElement("content_3").style.zIndex = 99;
    });
ul,li{
  padding:0px;
  margin:0px;
  list-style:none;
 }
 div#menu{
  width:100%;
  height:50px;
 }
 div#menu>ul li{
  float:left;
  border:1px solid #000;
  padding: 2px;
  background-color:blue;
  color:#fff;
  margin:5px;
  cursor:pointer;
 }
  
 #content{
  width:100%;
  border:1px solid #000;
  height:200px;
  position :relative;
 }
 
 .abs{
  position:absolute;
  width:100px;
  height:100px;
  border:1px solid #000;
  display:none;
 }
 
 #content_1{
  background-color:red;
 }
 #content_2{
  background-color:green;
 }
 #content_3{
  background-color:yellow;
 }
<div id=menu>
  <ul>
    <li id="item_1">menu item1</li>
    <li id="item_2">menu item2</li>
    <li id="item_3">menu item3</li>
  </ul>
</div>
<div id="content">
  <div id="content_1" class="abs"></div>
  <div id="content_2" class="abs"></div>
  <div id="content_3" class="abs"></div>
</div>

答案 1 :(得分:0)

只需获取其他元素并将其隐藏在js代码中:

&#13;
&#13;
function getElement(e) {
   var elem = document.getElementById(e);
   return elem;
}

getElement("item_1").addEventListener("click",function(){
    getElement("content_1").style.display = "block";
    getElement("content_2").style.display = "none";
    getElement("content_3").style.display = "none";    
    });
    
getElement("item_2").addEventListener("click",function(){
    getElement("content_1").style.display = "none";
    getElement("content_2").style.display = "block";
    getElement("content_3").style.display = "none";   
    });
    
getElement("item_3").addEventListener("click",function(){
    getElement("content_1").style.display = "none";
    getElement("content_2").style.display = "none";
    getElement("content_3").style.display = "block";   
    });
&#13;
ul,li{
  padding:0px;
  margin:0px;
  list-style:none;
 }
 div#menu{
  width:100%;
  height:50px;
 }
 div#menu>ul li{
  float:left;
  border:1px solid #000;
  padding: 2px;
  background-color:blue;
  color:#fff;
  margin:5px;
  cursor:pointer;
 }
  
 #content{
  width:100%;
  border:1px solid #000;
  height:200px;
  position :relative;
 }
 
 .abs{
  position:absolute;
  width:100px;
  height:100px;
  border:1px solid #000;
  display:none;
 }
 
 #content_1{
  background-color:red;
 }
 #content_2{
  background-color:green;
 }
 #content_3{
  background-color:yellow;
 }
&#13;
<div id=menu>
  <ul>
    <li id="item_1">menu item1</li>
    <li id="item_2">menu item2</li>
    <li id="item_3">menu item3</li>
  </ul>
</div>
<div id="content">
  <div id="content_1" class="abs"></div>
  <div id="content_2" class="abs"></div>
  <div id="content_3" class="abs"></div>
</div>
&#13;
&#13;
&#13;

希望这就是你要找的东西。

答案 2 :(得分:0)

这是你想要的

function getElement(e) {
   var elem = document.getElementById(e);
   return elem;
}

$("[id^=item_]").on('click', function () {
        $("[id^=content_]").css("display", "block");
        var id = $(this).prop('id');
        var dataArray = id.split('_');
        var numToCLick = parseInt(dataArray[1]);

        for(var i = 1; i < numToCLick; i++){
          getElement("content_"+i).style.display = "none";
        }
});
ul,li{
  padding:0px;
  margin:0px;
  list-style:none;
 }
 div#menu{
  width:100%;
  height:50px;
 }
 div#menu>ul li{
  float:left;
  border:1px solid #000;
  padding: 2px;
  background-color:blue;
  color:#fff;
  margin:5px;
  cursor:pointer;
 }
  
 #content{
  width:100%;
  border:1px solid #000;
  height:200px;
  position :relative;
 }
 
 .abs{
  //position:absolute;
  width:100px;
  height:100px;
  border:1px solid #000;
  float:left;
  display: none;
  
 }
 
 #content_1{
  background-color:red;
 }
 #content_2{
  background-color:green;
 }
 #content_3{
  background-color:yellow;
 }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<div id=menu>
  <ul>
    <li id="item_1">menu item1</li>
    <li id="item_2">menu item2</li>
    <li id="item_3">menu item3</li>
  </ul>
</div>
<div id="content">
  <div id="content_1" class="abs"></div>
  <div id="content_2" class="abs"></div>
  <div id="content_3" class="abs"></div>
</div>

希望这可以解决您的问题

答案 3 :(得分:0)

这只是jq版本。您可以根据abs

的ID选择li

&#13;
&#13;
let item = $("ul li"),
  content = $(".abs")

item.on("click", function() {
  let itemId = $(this).attr("id"),
    idNr = itemId.split('_').pop();
    
  content.removeClass("show")
  $("#content_" + idNr).addClass("show")

})
&#13;
ul,
li {
  padding: 0px;
  margin: 0px;
  list-style: none;
}

div#menu {
  width: 100%;
  height: 50px;
}

div#menu>ul li {
  float: left;
  border: 1px solid #000;
  padding: 2px;
  background-color: blue;
  color: #fff;
  margin: 5px;
  cursor: pointer;
}

#content {
  width: 100%;
  border: 1px solid #000;
  height: 200px;
  position: relative;
}

.abs {
  position: absolute;
  width: 100px;
  height: 100px;
  border: 1px solid #000;
  display: none;
}

#content_1 {
  background-color: red;
}

#content_2 {
  background-color: green;
}

#content_3 {
  background-color: yellow;
}

.abs.show {
  display: block;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id=menu>
  <ul>
    <li id="item_1">menu item1</li>
    <li id="item_2">menu item2</li>
    <li id="item_3">menu item3</li>
  </ul>
</div>
<div id="content">
  <div id="content_1" class="abs"></div>
  <div id="content_2" class="abs"></div>
  <div id="content_3" class="abs"></div>
</div>
&#13;
&#13;
&#13;