添加事件侦听器的多个函数

时间:2018-06-07 13:21:09

标签: javascript addeventlistener

没有jquery

喂!我正在尝试向按钮添加多个添加事件侦听器。

 function logoContame(){
    var logo = document.getElementById("logoheader");
    logo.addEventListener("click", hideDivHistorias);
    logo.addEventListener("click", showDivPagina2);
 }



function showPagina2(){

var historias = document.getElementById("historias");

var hist1 = document.getElementById("hist1");
hist1.id="hist1";
hist1.style.opacity = "0.0";
historias.appendChild(hist1);

var hist2 = document.getElementById("hist2");
hist2.id="hist2";
hist2.style.opacity = "0.0";
historias.appendChild(hist2);

var hist3 = document.getElementById("hist3");
hist3.id="hist3";
hist3.style.opacity = "0.0";
historias.appendChild(hist3);

var textoabrigo= document.getElementById("textoabrigo");
textoabrigo.id="textoabrigo";
textoabrigo.style.opacity = "0.0";
historias.appendChild(textoabrigo);

showHeader();
startFadeInPagina2();
showDivPagina2();
menuPag1();
logoInicial();

}


//Criar Página Inicial 


function showDivPagina2(){

var div2 = document.getElementById('historias').style.visibility = 
"visible";

}

第一个有效,但第二个无效。我怎样才能解决这个问题?我一直收到这个错误:

Uncaught TypeError: Cannot set property 'id' of null
at showPagina2 (code.js:290)
at HTMLButtonElement.logo1 (code.js:30) 

它与" hist1"有关。元件。

谢谢!

1 个答案:

答案 0 :(得分:0)

取决于此this answer

var logo = document.getElementById("logoheader");

logo.addEventListener('click',() => {    
     hideDivHistorias();
     showDivPagina2();    
});