我无法理解代码的执行
I am unable to understand the execution of the code
我的代码:
<head>
<!-- All the required scripts and CSS -->
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
<script type="text/javascript" src="http://www.parsecdn.com/js/parse-1.1.14.min.js"></script>
</head>
<body>
<!-- Bootstrap nav-tabs -->
<ul class="nav nav-tabs" id="mainTabs">
<li class="active" ><a href="#home" data-toggle="tab"><span class="glyphicon glyphicon-home"></span> Home</a></li>
<li><a href="#group" data-toggle="tab" >Groups</a></li>
</ul>
<!-- Content of nav-tabs -->
<div class="tab-content" id="myTabContent">
<div class="tab-pane fade active in" id="home">
home
</div>
<div class=" fade tab-pane" id="group" >
<div class="container">
<div class="row">
<div class="col-xs-12 col-md-12">
<h4>Groups</h4>
<button onclick="f5()"> Add Group</button>
</div>
</div>
</div>
</div>
</div>
<!-- Bootstrap Model -->
<div class="modal fade" id="myModal2">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<h4 class="modal-title" >Add New Group</h4>
</div>
<div class="modal-body">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button type="button" class="btn btn-primary btn-md" id="add" >Add</button>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
<script type="text/javascript">
function f5() {
$('#myModal2').modal('show');
$('#myModal2').on('click','#add', function (e) {
console.log("new clicked");
$('#myModal2').modal('hide');
});
}
</script>
</body>
现在的问题是:
当我第一次点击 ID 为 "add" 的模型按钮时:
Output: new clicked (printed one time)
当我再次点击 ID 为 "add" 的模型按钮时:
Now, Output: new clicked (printed two times)
当我再次点击 ID 为 "add" 的模型按钮时:
Now, Output: new clicked (printed three times)
当我再次点击 ID 为 "add" 的模型按钮时:
Now, Output: new clicked (printed four times)
为什么会这样??我搜索了堆栈溢出和 google 但找不到对我有用的东西。我是 bootstrap 的新手,我想要的是每次单击 ID 为 "add" 的按钮时 "new clicked" 应该只打印一次。
您需要检查 .off 方法表单 jQuery。
您似乎为同一件事添加了多个侦听器,并且您可能希望在重新分配之前删除所有这些侦听器以避免同一消息的多次打印。
例如,在您的 f5 函数中(为了代码清晰起见,请从现在开始使用 clear 函数)您每次执行时都会为同一元素创建一个新的侦听器。
.off() 会帮助你,因为在附加一个新的事件监听器之前,它会删除之前添加的监听器。
但是,为了以正确的方式做事,您应该将事件侦听器附加代码分组到单个方法中,例如 "addEventListeners()",并且 运行 该功能一次。 (如果您在加载页面后向页面添加元素,请使用事件委托)。
function f5() {
$('#myModal2').modal('show');
$('#myModal2').off('click'); // will remove any previous listeners attached with on
$('#myModal2').on('click','#add', function (e) {
console.log("new clicked");
$('#myModal2').modal('hide');
});
}
此致
我的代码:
<head>
<!-- All the required scripts and CSS -->
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
<script type="text/javascript" src="http://www.parsecdn.com/js/parse-1.1.14.min.js"></script>
</head>
<body>
<!-- Bootstrap nav-tabs -->
<ul class="nav nav-tabs" id="mainTabs">
<li class="active" ><a href="#home" data-toggle="tab"><span class="glyphicon glyphicon-home"></span> Home</a></li>
<li><a href="#group" data-toggle="tab" >Groups</a></li>
</ul>
<!-- Content of nav-tabs -->
<div class="tab-content" id="myTabContent">
<div class="tab-pane fade active in" id="home">
home
</div>
<div class=" fade tab-pane" id="group" >
<div class="container">
<div class="row">
<div class="col-xs-12 col-md-12">
<h4>Groups</h4>
<button onclick="f5()"> Add Group</button>
</div>
</div>
</div>
</div>
</div>
<!-- Bootstrap Model -->
<div class="modal fade" id="myModal2">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<h4 class="modal-title" >Add New Group</h4>
</div>
<div class="modal-body">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button type="button" class="btn btn-primary btn-md" id="add" >Add</button>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
<script type="text/javascript">
function f5() {
$('#myModal2').modal('show');
$('#myModal2').on('click','#add', function (e) {
console.log("new clicked");
$('#myModal2').modal('hide');
});
}
</script>
</body>
现在的问题是:
当我第一次点击 ID 为 "add" 的模型按钮时:
Output: new clicked (printed one time)
当我再次点击 ID 为 "add" 的模型按钮时:
Now, Output: new clicked (printed two times)
当我再次点击 ID 为 "add" 的模型按钮时:
Now, Output: new clicked (printed three times)
当我再次点击 ID 为 "add" 的模型按钮时:
Now, Output: new clicked (printed four times)
为什么会这样??我搜索了堆栈溢出和 google 但找不到对我有用的东西。我是 bootstrap 的新手,我想要的是每次单击 ID 为 "add" 的按钮时 "new clicked" 应该只打印一次。
您需要检查 .off 方法表单 jQuery。 您似乎为同一件事添加了多个侦听器,并且您可能希望在重新分配之前删除所有这些侦听器以避免同一消息的多次打印。
例如,在您的 f5 函数中(为了代码清晰起见,请从现在开始使用 clear 函数)您每次执行时都会为同一元素创建一个新的侦听器。 .off() 会帮助你,因为在附加一个新的事件监听器之前,它会删除之前添加的监听器。
但是,为了以正确的方式做事,您应该将事件侦听器附加代码分组到单个方法中,例如 "addEventListeners()",并且 运行 该功能一次。 (如果您在加载页面后向页面添加元素,请使用事件委托)。
function f5() {
$('#myModal2').modal('show');
$('#myModal2').off('click'); // will remove any previous listeners attached with on
$('#myModal2').on('click','#add', function (e) {
console.log("new clicked");
$('#myModal2').modal('hide');
});
}
此致