按我想要的方式放置我的div

时间:2018-05-07 09:04:26

标签: javascript html css

我想把两个盒子放在左边或者根据我的需要多放到底部,但是当我修复样式left:20px;时,它不会改变任何东西。

我从https://www.w3schools.com/howto/tryit.asp?filename=tryhow_js_todo获取此样本。我可以使这些表/框变小,但是因为我想将它们放在我想要的页面中它不会改变。 CSS有问题吗?

// Create a "close" button and append it to each list item
var myNodelist = document.getElementsByTagName("LI");
var i;
for (i = 0; i < myNodelist.length; i++) {
  var span = document.createElement("SPAN");
  var txt = document.createTextNode("\u00D7");
  span.className = "close";
  span.appendChild(txt);
  myNodelist[i].appendChild(span);
}

// Click on a close button to hide the current list item
var close = document.getElementsByClassName("close");
var i;
for (i = 0; i < close.length; i++) {
  close[i].onclick = function() {
    var div = this.parentElement;
    div.style.display = "none";
  }
}

// Create a new list item when clicking on the "Add" button
function newElement1() {
  var li = document.createElement("li");
  var inputValue = document.getElementById("myInput1").value;
  var t = document.createTextNode(inputValue);
  li.appendChild(t);
  if (inputValue === '') {
    alert("You must write something!");
  } else {
    document.getElementById("myUL1").appendChild(li);
  }
  document.getElementById("myInput1").value = "";

  var span = document.createElement("SPAN");
  var txt = document.createTextNode("\u00D7");
  span.className = "close";
  span.appendChild(txt);
  li.appendChild(span);

  for (i = 0; i < close.length; i++) {
    close[i].onclick = function() {
      var div = this.parentElement;
      div.style.display = "none";
    }
  }
}

function newElement2() {
	  var li = document.createElement("li");
	  var inputValue = document.getElementById("myInput2").value;
	  var t = document.createTextNode(inputValue);
	  li.appendChild(t);
	  if (inputValue === '') {
	    alert("You must write something!");
	  } else {
	    document.getElementById("myUL2").appendChild(li);
	  }
	  document.getElementById("myInput2").value = "";

	  var span = document.createElement("SPAN");
	  var txt = document.createTextNode("\u00D7");
	  span.className = "close";
	  span.appendChild(txt);
	  li.appendChild(span);

	  for (i = 0; i < close.length; i++) {
	    close[i].onclick = function() {
	      var div = this.parentElement;
	      div.style.display = "none";
	    }
	  }
	}
body {
  margin: auto;
  min-width: 250px;
}

/* Include the padding and border in an element's total width and height */
* {
  box-sizing: border-box;
}

/* Remove margins and padding from the list */
ul {
  margin: 0;
  padding: 0;
}

/* Style the list items */
ul li {
  cursor: pointer;
  position: relative;
  padding: 12px 8px 12px 40px;
  list-style-type: none;
  background: #eee;
  font-size: 18px;
  transition: 0.2s;
  
  /* make the list items unselectable */
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
}

/* Set all odd list items to a different color (zebra-stripes) */
ul li:nth-child(odd) {
  background: #f9f9f9;
}

/* Darker background-color on hover */
ul li:hover {
  background: #ddd;
}

/* When clicked on, add a background color and strike out text */
ul li.checked {
  background: #888;
  color: #fff;
  text-decoration: line-through;
}



/* Style the close button */
.close {
  position: absolute;
  right: 0px;
  top: 0;
  padding: 12px 16px 12px 16px;
}

.close:hover {
  background-color: #f44336;
  color: white;
}

/* Style the header */
.header {
  background-color: grey;
  padding: 30px 40px;
  color: white;
  text-align: center;
}

/* Clear floats after the header */
.header:after {
  content: "";
  display: table;
  clear: both;
}

/* Style the input */
input {
  border: none;
  width: 75%;
  padding: 10px;
  float: left;
  font-size: 16px;
}

/* Style the "Add" button */
.addBtn {
  padding: 10px;
  width: 25%;
  background: #d9d9d9;
  color: #555;
  float: left;
  text-align: center;
  font-size: 16px;
  cursor: pointer;
  transition: 0.3s;
}

.addBtn:hover {
  background-color: #bbb;
}

.footer {
    position: fixed;
    left: 0;
    bottom: 0;
    width: 100%;
}
<div id="myDIV" class="header" style="background-color: #000000;  width: 500px;">
  <h2 style="margin:5px">Primary tag</h2>
  <input type="text" id="myInput1" placeholder="">
  <span onclick="newElement1()" class="addBtn">Add</span>
</div>

<ul id="myUL1" style="width: 500px;">
</ul>

<br><br> <br><br> <br> <br>
<div id="myDIV" class="header"  style="width: 500px;">
  <h2 style="margin:5px">Secondary tags</h2>
  <input type="text" id="myInput2" placeholder="">
  <span onclick="newElement2()" class="addBtn">Add</span>
</div>

<ul id="myUL2" style="width: 500px;">
</ul>

1 个答案:

答案 0 :(得分:0)

  

当我修复样式“left:20px;”它不会改变任何东西

那是因为left: 20px不是你想要的,因为left属性通常用于改变具有绝对位置的元素的位置。

尝试margin属性。例如,margin-left: 20px会阻止您向右移动