使用 "innerHTML" 时的奇怪错误..?
Weird bug when "innerHTML" is used..?
我发现了一个我无法理解的奇怪错误。有人可以解释为什么下面的代码不起作用吗?当我将 Button2 放入 <div id='test'>
.
中时,它停止工作
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>
<button id='button1'>Button1</button>
<div id='test'>
<button id='button2'>Button2</button>
</div>
<script>
//Global var
var content = document.getElementById('test');
$(document).ready(function(){
$('#button1').on('click', function(){
content.innerHTML += '<hr>';
});
});
$(document).ready(function(){
$('#button2').on('click', function(){
content.innerHTML += ' Test';
});
});
</script>
</body>
</html>
当您更改元素时,"event" 不再有效。从上层元素进料,留在"event".
$('#button2').on('click', function(){
replace width:
$('body').on('click', '#button2', function () {
我发现了一个我无法理解的奇怪错误。有人可以解释为什么下面的代码不起作用吗?当我将 Button2 放入 <div id='test'>
.
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>
<button id='button1'>Button1</button>
<div id='test'>
<button id='button2'>Button2</button>
</div>
<script>
//Global var
var content = document.getElementById('test');
$(document).ready(function(){
$('#button1').on('click', function(){
content.innerHTML += '<hr>';
});
});
$(document).ready(function(){
$('#button2').on('click', function(){
content.innerHTML += ' Test';
});
});
</script>
</body>
</html>
"event" 不再有效。从上层元素进料,留在"event".
$('#button2').on('click', function(){
replace width:
$('body').on('click', '#button2', function () {