Beginner jQuery encountered such a right problem
Look at the error code first
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<div>
<button id="btn">测试</button>
<br />
<input type="text" name="msg1" /><br />
<input type="text" name="msg2" /><br />
</div>
<script type="text/javascript" src="js/jquery-1.12.3.js"></script>
<script type="text/javascript">
$(function (){
$("#btn").click(function (){
/* alert(this.innerHTML); */
alert($(this).html());
$("<input type="text" name="msg3"/><br />").appendTo('div');
})
})
</script>
</body>
</html>
At first glance, I think there is no problem, just insert an input into the div
but it runs and an error is reported, as shown in the figure:
Reason for error:
quotation marks are not used correctly, double quotation marks in js cannot be nested double quotation marks, single quotation marks cannot be nested single quotation marks
Solution:
Change the outermost double quotation mark to single quotation mark
Correct code:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<div>
<button id="btn">测试</button>
<br />
<input type="text" name="msg1" /><br />
<input type="text" name="msg2" /><br />
</div>
<script type="text/javascript" src="js/jquery-1.12.3.js"></script>
<script type="text/javascript">
$(function (){
$("#btn").click(function (){
/* alert(this.innerHTML); */
alert($(this).html());
$('<input type="text" name="msg3"/><br />').appendTo('div');
})
})
</script>
</body>
</html>