为什么我在单击“p”元素时调用.toggle()时两次收到警报“toggle complete”。以下是示例代码
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("p").toggle( 'slow', function(){
alert('toggel complete');
});
});
});
</script>
</head>
<body>
<button>Toggle</button>
<p>This is a paragraph with little content.</p>
<p>This is another small paragraph.</p>
</body>
</html>
答案 0 :(得分:3)
您正在添加影响所有<p>
代码的点击事件。在您的情况下,您正在添加两个事件,因此您的警报将被调用两次。
答案 1 :(得分:0)
代码中有两个'p'
元素。
这就是alert()
显示两次的原因。