[英]What's wrong with this JavaScript code?
var menu = {
minibox: document.getElementById("hypt_opContainer"),
miniboxoptions: [document.getElementById("hypt_op"),
document.getElementById("style_op"), document.getElementById("js_op")]
};
Object.prototype = {
toggleVisibility: function(e) {
e.style.display = (e.style.display == "none") ? "block" : "none";
}
};
menu.miniboxoptions[0].addEventListener("click", menu.minibox.toggleVisibility);
當我單擊該框時,它沒有任何作用。
怎么了?
2個問題:並非所有瀏覽器都支持addEventListener
和中的參數e
toggleVisibility: function(e) {...
不是元素。 這是一個事件對象,因此e.srcElement
應該再次使用-大多數瀏覽器。 更多: https : //developer.mozilla.org/en/DOM/element.addEventListener
提示:切勿觸摸Object.prototype!
我不知道以這種方式創建原型是否有效。 清楚地寫出它的目的是要替換原型而不是擴展它。
我會寫
Object.prototype.toggleVisibility = function(e) {
e.style.display = (e.style.display == "none") ? "block" : "none";
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.