jQuery .append()函数错误

时间:2016-12-10 05:26:10

标签: javascript jquery

我正面临一个错误(哎呀,再试一次。确保使用django-subdomains将您的项目添加到您的身上)。当我尝试.append()它的工作正常,但我找不到解决方案或原因.append()函数说两个句号而不是一个句号。如果有人能给我一个解决方案,对我来说将是一个很大的帮助。



$('<div class="item">' + toAdd + '</div>')..append('.list');
&#13;
$(document).ready(function(){
   $('#button').click(function(){
       var toAdd = $('input[name=checkListItem]').val();
       $('<div class="item">' + toAdd + '</div>').append('.list')

   });
 
});
&#13;
h2 {
    font-family:arial;
}

form {
    display: inline-block;
}

#button{
    display: inline-block;
    height:20px;
	width:70px;
	background-color:#cc0000;
	font-family:arial;
	font-weight:bold;
	color:#ffffff;
	border-radius: 5px;
	text-align:center;
	margin-top:2px;
}

.list {
	font-family:garamond;
	color:#cc0000;
}
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:1)

&#13;
&#13;
$(document).ready(function(){
   $('#button').click(function(){

       var toAdd = $('input[name=checkListItem]').val();
   
       $('div.list').append('<div class="item">' + toAdd + '</div>')

   });
 
});
&#13;
h2 {
    font-family:arial;
}

form {
    display: inline-block;
}

#button{
    display: inline-block;
    height:20px;
	width:70px;
	background-color:#cc0000;
	font-family:arial;
	font-weight:bold;
	color:#ffffff;
	border-radius: 5px;
	text-align:center;
	margin-top:2px;
}

.list {
	font-family:garamond;
	color:#cc0000;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
<html>
    <head>
    	<title>To Do</title>
        <link rel="stylesheet" type="text/css" href="stylesheet.css"/>
        <script type="text/javascript" src="script.js"></script>
	</head>
	<body>
		<h2>To Do</h2>
		<form name="checkListForm">
			<input type="text" name="checkListItem"/>
		</form>
		<div id="button">Add!</div>
		<br/>
		<div class="list"></div>
	</body>
</html>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

喜欢这个?

&#13;
&#13;
$(document).ready(function(){
   $('#button').click(function(){
       var toAdd = $('input[name=checkListItem]').val();
       $('<div />',{html:toAdd,class:"item"}).appendTo('.list')

   });
 
});
&#13;
h2 {
    font-family:arial;
}

form {
    display: inline-block;
}

#button{
    display: inline-block;
    height:20px;
	width:70px;
	background-color:#cc0000;
	font-family:arial;
	font-weight:bold;
	color:#ffffff;
	border-radius: 5px;
	text-align:center;
	margin-top:2px;
}

.list {
	font-family:garamond;
	color:#cc0000;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
<html>
    <head>
    	<title>To Do</title>
        <link rel="stylesheet" type="text/css" href="stylesheet.css"/>
        <script type="text/javascript" src="script.js"></script>
	</head>
	<body>
		<h2>To Do</h2>
		<form name="checkListForm">
			<input type="text" name="checkListItem"/>
		</form>
		<div id="button">Add!</div>
		<br/>
		<div class="list"></div>
	</body>
</html>
&#13;
&#13;
&#13;