object使用append的对象问题

时间:2017-07-23 12:06:27

标签: javascript jquery function append

我的目标是创建一个待办事项列表,并有可能在列表中添加更多项目。

我使用.append()方法添加内容。但是,我还需要为添加的每个新项添加一个复选框。

这是我到目前为止所拥有的

(运行代码段并添加新项目,您将看到我遇到的错误)



$(document).ready(function() {
	$("#button").click(function() {
		var check = $("<input>", { type: "checkbox" });
		var toAdd = $("input[name=ListItem]").val();
		$("ul").append($("<li>" + check + "<label>" + toAdd + "</label>" + "</li>"));
	});
});
&#13;
@import url('https://fonts.googleapis.com/css?family=Josefin+Sans');
ul {
	list-style: none;
	margin: 0;
	padding: 0;
}
li {
	margin: 50px 0;
	font-family: 'Josefin Sans', sans-serif;
	font-size: 20px;
	font-weight: bolder;
}
input {
	cursor: pointer;
	position: relative;
	visibility: hidden;
}
input:after {
	border: 3px solid lightblue;
	border-radius: 50%;;
	content: "";
	display: block;
	height: 46px;
	width: 46px;
	text-align: center;
	visibility: visible;
	transition: all 100ms ease-in-out;
}
input:checked:after {
	border: 3px solid lightblue;
	color: white;
	font-size: 30px;
	border-radius: 50%;
	content: "✘";
	line-height: 43px;
	background-color: lightblue;
	animation: bounce 300ms ease-in-out forwards;
}
@keyframes bounce {
	0% {transform: scale(1, 1);}
	70% {transform: scale(1.1, 1.1);}
	100% {transform: scale(1, 1);}
}
label {
	position: relative;
	left: 50px;
	top: 18px;!important
}
input[type=text] {
	border: 3px solid lightblue;
	width: 100px;
	height: 50px;
	visibility: visible;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="list">
 <h1>To do list</h1>
 <form name="toDoList">
  <input type="text" name="ListItem" />
 </form>
 <div id="button">Add</div>
 <ul>
  <li>
   <input type="checkbox">
   <label for="checkbox">Item1</label>
  </li>
  <li>
   <input type="checkbox">
   <label for="checkbox">Item2</label>
  </li>
  <li>
   <input type="checkbox">
   <label for="checkbox">Item3</label>
  </li>
 </ul>
</div>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:0)

您将Object追加到string,因此错误。以下是您的代码的更新工作版本:

$(document).ready(function() {
  $("#button").click(function() {
    var toAdd = $("input[name=ListItem]").val();
    $("ul").append($("<li><input type='checkbox'><label>" + toAdd + "</label>" + "</li>"));
  });
});
@import url('https://fonts.googleapis.com/css?family=Josefin+Sans');
ul {
  list-style: none;
  margin: 0;
  padding: 0;
}

li {
  margin: 50px 0;
  font-family: 'Josefin Sans', sans-serif;
  font-size: 20px;
  font-weight: bolder;
}

input {
  cursor: pointer;
  position: relative;
  visibility: hidden;
}

input:after {
  border: 3px solid lightblue;
  border-radius: 50%;
  ;
  content: "";
  display: block;
  height: 46px;
  width: 46px;
  text-align: center;
  visibility: visible;
  transition: all 100ms ease-in-out;
}

input:checked:after {
  border: 3px solid lightblue;
  color: white;
  font-size: 30px;
  border-radius: 50%;
  content: "✘";
  line-height: 43px;
  background-color: lightblue;
  animation: bounce 300ms ease-in-out forwards;
}

@keyframes bounce {
  0% {
    transform: scale(1, 1);
  }
  70% {
    transform: scale(1.1, 1.1);
  }
  100% {
    transform: scale(1, 1);
  }
}

label {
  position: relative;
  left: 50px;
  top: 18px;
  !important
}

input[type=text] {
  border: 3px solid lightblue;
  width: 100px;
  height: 50px;
  visibility: visible;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="list">
  <h1>To do list</h1>
  <form name="toDoList">
    <input type="text" name="ListItem" />
  </form>
  <div id="button">Add</div>
  <ul>
    <li>
      <input type="checkbox">
      <label for="checkbox">Item1</label>
    </li>
    <li>
      <input type="checkbox">
      <label for="checkbox">Item2</label>
    </li>
    <li>
      <input type="checkbox">
      <label for="checkbox">Item3</label>
    </li>
  </ul>
</div>

答案 1 :(得分:0)

保持大部分代码不变,使用.append函数修复get(0).outerHTML()命令就足够了,如下所示:

$("ul").append($("<li>" + check.get(0).outerHTML() + "<label>" + toAdd + "</label>" + "</li>"));

函数get(0)选择对象中的第一个(也是唯一的)元素,.outerHTML()从jQuery对象生成HTML。

以下是工作片段:

&#13;
&#13;
$(document).ready(function() {
	$("#button").click(function() {
		var check = $("<input>", { type: "checkbox" });
		var toAdd = $("input[name=ListItem]").val();
alert(check.get(0).outerHTML);
		$("ul").append($("<li>" + check.get(0).outerHTML + "<label>" + toAdd + "</label>" + "</li>"));
	});
});
&#13;
@import url('https://fonts.googleapis.com/css?family=Josefin+Sans');
ul {
	list-style: none;
	margin: 0;
	padding: 0;
}
li {
	margin: 50px 0;
	font-family: 'Josefin Sans', sans-serif;
	font-size: 20px;
	font-weight: bolder;
}
input {
	cursor: pointer;
	position: relative;
	visibility: hidden;
}
input:after {
	border: 3px solid lightblue;
	border-radius: 50%;;
	content: "";
	display: block;
	height: 46px;
	width: 46px;
	text-align: center;
	visibility: visible;
	transition: all 100ms ease-in-out;
}
input:checked:after {
	border: 3px solid lightblue;
	color: white;
	font-size: 30px;
	border-radius: 50%;
	content: "✘";
	line-height: 43px;
	background-color: lightblue;
	animation: bounce 300ms ease-in-out forwards;
}
@keyframes bounce {
	0% {transform: scale(1, 1);}
	70% {transform: scale(1.1, 1.1);}
	100% {transform: scale(1, 1);}
}
label {
	position: relative;
	left: 50px;
	top: 18px;!important
}
input[type=text] {
	border: 3px solid lightblue;
	width: 100px;
	height: 50px;
	visibility: visible;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="list">
 <h1>To do list</h1>
 <form name="toDoList">
  <input type="text" name="ListItem" />
 </form>
 <div id="button">Add</div>
 <ul>
  <li>
   <input type="checkbox">
   <label for="checkbox">Item1</label>
  </li>
  <li>
   <input type="checkbox">
   <label for="checkbox">Item2</label>
  </li>
  <li>
   <input type="checkbox">
   <label for="checkbox">Item3</label>
  </li>
 </ul>
</div>
&#13;
&#13;
&#13;