[英]How can I modify this function to add an eventListener?
我有一個模態的函數,如下所示:
+(function () {
'use strict';
/**
* @constructor
*/
var Modal = function (target) {
this.$target = document.querySelector(target);
this.$dismiss = this.$target.querySelectorAll('[data-dismiss="modal"]');
this.$backdrop = null;
this.isVisible = false;
};
Modal.prototype.toggle = function () {
return this[!this.isVisible ? 'show' : 'hide']();
};
Modal.prototype.show = function () {
if (this.isVisible) {
return;
}
var me = this;
this.isVisible = true;
// Disable scrolling of content behind the modal
document.body.classList.toggle('modal-open');
this.escape();
// Add close events
for (var i = 0; i < this.$dismiss.length; i++) {
this.$dismiss[i].addEventListener('click', this.hide.bind(this));
}
this.backdrop(function () {
me.$target.style.display = 'block';
me.$target.scrollTop = 0;
me.$target.setAttribute('aria-hidden', false);
// Modal must be the focused element
me.$target.focus();
});
};
Modal.prototype.hide = function (e) {
if (e) {
e.preventDefault();
}
if (!this.isVisible) {
return;
}
var me = this;
this.isVisible = false;
// Enable scrolling of content behind the modal
document.body.classList.toggle('modal-open');
this.escape();
// Remove close events
for (var i = 0; i < this.$dismiss.length; i++) {
this.$dismiss[i].removeEventListener('click', this.hide.bind(this));
}
this.$target.setAttribute('aria-hidden', true);
this.$target.style.display = 'none';
this.backdrop();
};
Modal.prototype.backdrop = function (callback) {
if (this.isVisible && !this.$backdrop) {
// Create backdrop
this.$backdrop = document.createElement('div');
this.$backdrop.className = 'modal-backdrop fade';
document.body.appendChild(this.$backdrop);
// Backdrop fade in
this.$backdrop.classList.add('in');
} else {
// Remove backdrop
this.$backdrop.parentNode.removeChild(this.$backdrop);
this.$backdrop = null;
}
if (callback) {
callback();
}
};
Modal.prototype.keyDown = function (e) {
this.isVisible && (e.keyCode === 27) && this.hide();
};
Modal.prototype.escape = function () {
this.isVisible ?
this.$target.addEventListener('keydown', this.keyDown.bind(this)) :
this.$target.removeEventListener('keydown', this.keyDown.bind(this));
};
Modal.init = function () {
var modal,
target,
triggers = document.querySelectorAll('[data-modal]');
// Attach click event to modal trigger elements
for (var i = 0; i < triggers.length; i++) {
triggers[i].addEventListener('click', function (e) {
target = this.getAttribute('data-modal');
if (e) { e.preventDefault(); }
modal = new Modal(target);
modal.toggle();
}, false);
}
};
// Self initialization
document.addEventListener('DOMContentLoaded', function () {
Modal.init();
}, false);
$(document).ready(function() {
setInterval(function() {
Modal.init();
}, 4000);
});
})();
我不想像底部那樣通過setInterval調用Modal.init(),而是要定位鏈接的ID(“ tab_button”)並在單擊它時觸發Modal.init()。 我以為可以添加以下內容:
var tab_button = document.getElementById("tab_button");
if (tab_button != null) {
tab_button.addEventListener('click', function() {
console.log("Tab button was clicked");
Modal.init();
}, false);
}
但單擊它不會觸發日志/調用。 有任何想法嗎?
這是在Ruby on Rails應用程序上運行的javascript。
我建議只將您的點擊處理程序連接到文檔,以便所有點擊事件都會冒泡並在一個地方處理。 否則,您每次修改頁面內容(例如使用AJAX)時,可能都需要重新附加點擊處理程序。 例如,假設您有一個鏈接,其ID為“ tab_button”,如下所示:
<a href="..." id="tab_button">...</a>
您可以像這樣添加點擊處理程序:
$(document).on('click', '#tab_button', function(event) {
event.preventDefault();
console.log('Tab button was clicked');
Modal.init();
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.