如何在onclick函数Javascript html中更改onclick函数

时间:2018-06-20 14:08:04

标签: javascript html

<img src="Imagens/R5.jpg" id="Image1" class="P1" value="R5" style="width:164px;height:164px;margin: 0 auto;" onclick="pergunta1(this)" >
<img src="Imagens/R2.jpg" id="Image2" class="P1" value="R2" style="width:163px;height:163px;margin: 0 auto;" onclick="pergunta1(this)" >
<img src="Imagens/R8.jpg" id="Image3" class="P1" value="R8" style="width:163px;height:163px;margin: 0 auto;" onclick="pergunta1(this)" >

我正在尝试更改图像鼠标的onclick功能

function pergunta1(element){
        var res="";
    if (element.getAttribute("value") == "R5"){
        res= "N1_5"
    }
    else if (element.getAttribute("value") == "R2"){
        res="N1_2"
    }
    else{
        res ="N1_8"
    }
    document.getElementById("resultado").innerHTML =res;
    document.getElementById('Image1').src='Imagens/R7.jpg'
    document.getElementById('Image2').src='Imagens/R1.jpg'
    document.getElementById('Image3').src='Imagens/R6.jpg'
    document.getElementById('Image1').value='R7'
    document.getElementById('Image2').value='R1'
    document.getElementById('Image3').value='R6'
    document.getElementById('Image1').onclick = pergunta2(this);
    document.getElementById('Image2').onclick = "pergunta2(this)";
    document.getElementById( 'Image3' ).setAttribute( "onClick", "javascript: pergunta2(this);" );

}

我已经做过一些研究,尝试了3种不同的方法,但是似乎都没有作用

1 个答案:

答案 0 :(得分:0)

尝试;

document.getElementById('Image1').removeEventListener('click', pergunta1).addEventListener('click', pergunta2)