img onclick 函数

Img onclick function

我的图片很少,点击后,我想 运行 编码,比如提醒图片编号。

<img onclick="picture(1)" src="asd.jpg">
<img onclick="picture(2)" src="sdf.jpg">
<script>
   function picture(picnumber){
   alert(picnumber);
   }
</script> 

当我警告字符串而不是变量时,它起作用了。

编辑:很抱歉包含示例而不是我的实际代码,我认为没问题,下面给出的解决方案没有修复我的代码:

<body>
    <style>
        img{
            height:80px;
        }
    </style>
    <script>
        function picture(picbumber){
            alert(picnumber); 
        }
    </script>
    <h1>GALLERY</h1>
    <div class="photos">
        <img onclick="picture(1)" src="https://i.insider.com/5f5a5f37e6ff30001d4e8163?width=700"/>
        <img onclick="picture(2)" src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSChu52FnNKfSPRTMY8HIXei8leVAMvkURqqQ&usqp=CAU">
        <img onclick="picture(3)" src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSIvgVWXKciw_UZIuzVenY4QvmCbDQb43J1lQ&usqp=CAU">
        <img onclick="picture(4)" scr="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSY9xhvbRt8gXsEyO1dOW41asBHrEb2mZkRgQ&usqp=CAU">
        <img onclick="picture(5)" src="https://dkt6rvnu67rqj.cloudfront.net/cdn/ff/T8cy0-640W8sartvA9TWmv08NbGPFxsVvf8gFtBDE08/1577112797/public/styles/600x400/public/media/int_files/elephant_in_tanzania.jpg?h=f507d761&itok=Ei8OXcGi">
        <img onclick="picture(6)" src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcR1u8AKJBUZdSX7SIHvd8R1SiYyypx7elpstQ&usqp=CAU">
    </div>
</body>

只是改变顺序,在onclick中你正在执行一个尚未定义的函数否则

<script>
   function picture(picnumber){
   alert(picnumber);
   }
</script> 
<img onclick="picture(1)" src="asd.jpg">
<img onclick="picture(2)" src="sdf.jpg">

请在头部添加脚本代码。在这里查看我的演示。

<head>
    <script>
        function picture(picnumber){
            alert(picnumber)
        }
    </script>
</head>
<body>
    <img onclick="picture(1)" src="./asd.jpg" />
    <img onclick="picture(2)" src="./sdf.jpg" />
</body>
</html>```