1
Я хочу добавить некоторые функции к каждому элементу DOM, но я не хочу перебирать все элементы. Это демонстрационный код:наследовать прототип всем дочерним элементам в JavaScript
window.onload = function() {
/* get the constructor of document, which is Document
add the clickMe function to the prototype */
document.constructor.prototype.clickMe = function (func) {
this.addEventListener('click', function() {
func();
});
};
document.clickMe(clickDocument); // works
/* doesn't work, because the prototype of document.getElementById('random_btn')
does not have the function clickMe (makes sense) */
document.getElementById('random_btn').clickMe(clickRandomBtn);
}
function clickDocument() {
alert("clicked documet");
}
function clickRandomBtn() {
alert("clicked a random button");
}
Спасибо! Это то, что я искал. –