Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/446.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在HTML/Javascript中返回图像的值 _Javascript_Html - Fatal编程技术网

如何在HTML/Javascript中返回图像的值

如何在HTML/Javascript中返回图像的值 ,javascript,html,Javascript,Html,我希望它返回图像的值,然后隐藏它,但它甚至不返回任何值,我还使用JQuery,这样我就可以隐藏具有相同类的所有图像 <p id="resultado"></p> <img src="Imagens/R5.jpg" id="P1" class="P1" value="R5" onclick="pergunta1()" style="width:164px;height:164px;"> <img src="Imagens/R2.jpg" i

我希望它返回图像的值,然后隐藏它,但它甚至不返回任何值,我还使用JQuery,这样我就可以隐藏具有相同类的所有图像

<p id="resultado"></p>
    <img src="Imagens/R5.jpg" id="P1" class="P1" value="R5" onclick="pergunta1()" style="width:164px;height:164px;">
    <img src="Imagens/R2.jpg" id="P1" class="P1" value="R2" onclick="pergunta1()" style="width:163px;height:163px;">
    <img src="Imagens/R8.jpg" id="P1" class="P1" value="R8" onclick="pergunta1()" style="width:163px;height:163px;">

函数pergunta1(){
var p1=document.getElementById('p1');
如果(p1.value==“R5”){
返回“N1_5”
}
否则如果(p1.value==“R2”){
返回“N1_2”
}
否则{
返回“N1_8”
}
$('.P1').hide();
}
document.getElementById(“resultado”).innerHTML=myFunction();
大宗报价

最好只使用一种脚本语言,尽量不要混用,多学一些。这是你想要的代码


功能pergunta1(元件){ var res=“”; if(element.getAttribute(“value”)=“R5”){ res=“N1_5” } else if(element.getAttribute(“value”)=“R2”){ res=“N1_2” } 否则{ res=“N1_8” } element.style.display=“无”; document.getElementById(“resultado”).innerHTML=res; }
您需要将p resultado移动到函数内部以显示所选值

函数pergunta1(){
var valor=$(event.target).attr(“值”);
结果变量;
如果(价值=“R5”){
resultado=“N1_5”
}
否则如果(valor==“R2”){
resultado=“N1_2”
}
否则{
resultado=“N1_8”
}
$('.P1').hide();
$(“#resultado”).html(resultado);
}


同一文档中的重复ID是无效的HTML。首先试着解决这个问题。谢谢,我是javascript新手,所以我会把事情搞混
<script type="text/javascript">

    function pergunta1(){

    var p1 = document.getElementById('P1');
        if (p1.value == "R5"){
            return "N1_5"
        }
        else if (p1.value == "R2"){
            return "N1_2"
        }
        else{
            return "N1_8"
        }
        $('.P1').hide();
    }
    document.getElementById("resultado").innerHTML = myFunction();

</script>