gpt4 book ai didi

javascript - 当元素从 dom 中消失时,更改事件保持事件状态

转载 作者:行者123 更新时间:2023-12-04 07:42:43 35 4
gpt4 key购买 nike

在接下来的代码中,当按下 Set time按钮,使用 timepicker 使输入可见和 change事件启动,然后使用按钮 Go to another page 将其隐藏, 这个按钮用 ng-if 隐藏它但这会导致元素从 dom 中消失,并且启动的事件仍在等待,所以当按下按钮 Set time第二次,这不起作用,在同一 View 中,我用 ng-show 解决了它将输入隐藏在 dom 中,当我更改 View 并返回时出现问题,如何在调用之前再次删除或重新激活该事件?或者我可以将组件重新绑定(bind)到事件吗? 我留下了错误的副本。

var app = angular.module('app', []);

app.controller('ctrl',function($scope){


$scope.changevalue = () => {
$scope.setUp = true;
this.resetTime();

}

$scope.hide = () => {
$scope.setUp = false;
}

this.resetTime = () => {
setTimeout(function(){
var timepicker = new TimePicker('timePicker', {lang: 'en', theme: 'dark'});

timepicker.on('change', function(evt) {
// este evento solo sucede una vez
console.log("evt",evt)
var value = (evt.hour || '00') + ':' + (evt.minute || '00');
evt.element.value = value;
}); // fin on change
}, 200);// fin timeOut
}// fin resetTime

})
*,:after,:before{box-sizing:border-box;margin:0;padding:0}._jw-tpk-container{position:absolute;width:250px;height:140px;padding:0;background:#fff;font-family:inherit;font-weight:400;overflow:hidden;border-radius:3px;box-sizing:border-box;max-width:250px;margin-left:auto;margin-right:auto;line-height:1rem;font-size:1rem}._jw-tpk-container:after{content:" ";display:block;clear:both}._jw-tpk-container *,._jw-tpk-container :after,._jw-tpk-container :before{box-sizing:border-box}._jw-tpk-container *,._jw-tpk-container .active,._jw-tpk-container :focus,._jw-tpk-container :hover{text-decoration:none;outline:none}._jw-tpk-container._jw-tpk-dragging{opacity:.85!important}._jw-tpk-container._jw-tpk-dragging ._jw-tpk-header{cursor:-webkit-grabbing;cursor:grabbing}._jw-tpk-container ol{text-align:center;list-style-type:none}._jw-tpk-container ol>li{display:inline-block}._jw-tpk-container ol>li>a{display:inline-block;padding:3px 0;width:25px;color:inherit;border-radius:3px;border:1px solid transparent;font-size:1.2rem}._jw-tpk-container ol>li>a:not(._jw-tpk-selected):hover{cursor:pointer;border:1px solid #ccc;border-right:1px solid #aaa;border-bottom:1px solid #aaa;background:#f5f5f5;background:-webkit-linear-gradient(#e6e6e6,#f5f5f5);background:linear-gradient(#e6e6e6,#f5f5f5);box-shadow:0 2px 3px hsla(0,0%,86%,.8)}._jw-tpk-header{position:relative;font-weight:600;text-align:center;cursor:-webkit-grab;cursor:grab;line-height:1.875rem}._jw-tpk-header:after,._jw-tpk-header:before{content:"";display:table}._jw-tpk-header:after{clear:both}._jw-tpk-body{padding:2px 0}._jw-tpk-body:after,._jw-tpk-body:before{content:"";display:table}._jw-tpk-body:after{clear:both}._jw-tpk-hour{width:64.49275%;float:left;margin-right:1.44928%}._jw-tpk-minute{width:34.05797%;float:right;margin-right:0}._jw-tpk-dark{color:#212121;box-shadow:inset 0 0 0 1px #212121}._jw-tpk-dark ._jw-tpk-header,._jw-tpk-dark ol>li>a._jw-tpk-selected{color:#f5f5f5;background:-webkit-linear-gradient(#212121,#545454);background:linear-gradient(#212121,#545454)}._jw-tpk-blue-grey{color:#263238;box-shadow:inset 0 0 0 1px #263238}._jw-tpk-blue-grey ._jw-tpk-header,._jw-tpk-blue-grey ol>li>a._jw-tpk-selected{color:#cfd8dc;background:-webkit-linear-gradient(#263238,#4f6875);background:linear-gradient(#263238,#4f6875)}
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>
<script src="//cdn.jsdelivr.net/timepicker.js/latest/timepicker.min.js"></script>


<div ng-app="app">
<div ng-controller="ctrl">
<button name="button" ng-click="hide()">Go to another page</button>
<button name="button" ng-click="changevalue()">Set time</button>
<input type="text" class="form-control" id="timePicker" data-ng-if="setUp" ng-model="hour" autocomplete="off">
</div>
</div>

最佳答案

问题是您使用的是旧版本的包,它使用相同的 ID hour_list_idminute_list_id对于所有选择器元素并使用这些 id 来设置选择监听器。当您调用 new Timepicker再次,它创建了新的选择器元素,但仍然是相同的 id,因此句柄只是绑定(bind)到第一个选择器元素。这就是为什么当你选择时间时,change不触发。
下面是源代码中与此相关的代码:

...
var ids = {"hour_list":"hour_list_id","minute_list":"minute_list_id"};
...
var _VARS = Object.freeze({
...
ids: ids,
...
});
...
var VARS = _VARS;
...
var hour_list = utils.$(VARS.ids.hour_list);
var minute_list = utils.$(VARS.ids.minute_list);
...
this.collection.hours = utils.getAllChildren(hour_list, 'a');
this.collection.minutes = utils.getAllChildren(minute_list, 'a');

this.collection.hours.forEach(function (hour) {
hour.addEventListener('click', selectHour);
});
this.collection.minutes.forEach(function (minute) {
minute.addEventListener('click', selectMinute);
});
要解决此问题,您只需使用最新版本的软件包:

var app = angular.module('app', []);

app.controller('ctrl',function($scope){

$scope.changevalue = () => {
$scope.setUp = true;
this.resetTime();
}

$scope.hide = () => {
$scope.setUp = false;
}

this.resetTime = () => {
setTimeout(function(){
var timepicker = new TimePicker('#timePicker', {lang: 'en', theme: 'dark'});

timepicker.on('change', function(evt) {
// este evento solo sucede una vez
console.log("evt",evt)
var value = (evt.hour || '00') + ':' + (evt.minute || '00');
evt.element.value = value;
}); // fin on change
}, 200);// fin timeOut
}// fin resetTime

})
*,:after,:before{box-sizing:border-box;margin:0;padding:0}._jw-tpk-container{position:absolute;width:250px;height:140px;padding:0;background:#fff;font-family:inherit;font-weight:400;overflow:hidden;border-radius:3px;box-sizing:border-box;max-width:250px;margin-left:auto;margin-right:auto;line-height:1rem;font-size:1rem}._jw-tpk-container:after{content:" ";display:block;clear:both}._jw-tpk-container *,._jw-tpk-container :after,._jw-tpk-container :before{box-sizing:border-box}._jw-tpk-container *,._jw-tpk-container .active,._jw-tpk-container :focus,._jw-tpk-container :hover{text-decoration:none;outline:none}._jw-tpk-container._jw-tpk-dragging{opacity:.85!important}._jw-tpk-container._jw-tpk-dragging ._jw-tpk-header{cursor:-webkit-grabbing;cursor:grabbing}._jw-tpk-container ol{text-align:center;list-style-type:none}._jw-tpk-container ol>li{display:inline-block}._jw-tpk-container ol>li>a{display:inline-block;padding:3px 0;width:25px;color:inherit;border-radius:3px;border:1px solid transparent;font-size:1.2rem}._jw-tpk-container ol>li>a:not(._jw-tpk-selected):hover{cursor:pointer;border:1px solid #ccc;border-right:1px solid #aaa;border-bottom:1px solid #aaa;background:#f5f5f5;background:-webkit-linear-gradient(#e6e6e6,#f5f5f5);background:linear-gradient(#e6e6e6,#f5f5f5);box-shadow:0 2px 3px hsla(0,0%,86%,.8)}._jw-tpk-header{position:relative;font-weight:600;text-align:center;cursor:-webkit-grab;cursor:grab;line-height:1.875rem}._jw-tpk-header:after,._jw-tpk-header:before{content:"";display:table}._jw-tpk-header:after{clear:both}._jw-tpk-body{padding:2px 0}._jw-tpk-body:after,._jw-tpk-body:before{content:"";display:table}._jw-tpk-body:after{clear:both}._jw-tpk-hour{width:64.49275%;float:left;margin-right:1.44928%}._jw-tpk-minute{width:34.05797%;float:right;margin-right:0}._jw-tpk-dark{color:#212121;box-shadow:inset 0 0 0 1px #212121}._jw-tpk-dark ._jw-tpk-header,._jw-tpk-dark ol>li>a._jw-tpk-selected{color:#f5f5f5;background:-webkit-linear-gradient(#212121,#545454);background:linear-gradient(#212121,#545454)}._jw-tpk-blue-grey{color:#263238;box-shadow:inset 0 0 0 1px #263238}._jw-tpk-blue-grey ._jw-tpk-header,._jw-tpk-blue-grey ol>li>a._jw-tpk-selected{color:#cfd8dc;background:-webkit-linear-gradient(#263238,#4f6875);background:linear-gradient(#263238,#4f6875)}
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>
<script src="https://unpkg.com/timepicker.js@3.1.0/dist/timepicker.js"></script>

<div ng-app="app">
<div ng-controller="ctrl">
<button name="button" ng-click="hide()">Go to another page</button>
<button name="button" ng-click="changevalue()">Set time</button>
<input type="text" class="form-control" id="timePicker" data-ng-if="setUp" ng-model="hour" autocomplete="off">
</div>
</div>

关于javascript - 当元素从 dom 中消失时,更改事件保持事件状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67375275/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com