Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/461.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
使用javascript单击html按钮_Javascript_Html - Fatal编程技术网

使用javascript单击html按钮

使用javascript单击html按钮,javascript,html,Javascript,Html,我必须点击两次html按钮才能实现我在项目中所需的功能。因此,我使用javascript使用click()点击按钮。但是下面的脚本对我不起作用。请试试这个 单击灯泡以打开/关闭灯光 document.getElementById('myImage')。单击(); document.getElementById('myImage')。单击(); 函数changeImage(){ 警报(100); var image=document.getElementById('myImage'); if(i

我必须点击两次html按钮才能实现我在项目中所需的功能。因此,我使用javascript使用click()点击按钮。但是下面的脚本对我不起作用。请试试这个


单击灯泡以打开/关闭灯光

document.getElementById('myImage')。单击(); document.getElementById('myImage')。单击(); 函数changeImage(){ 警报(100); var image=document.getElementById('myImage'); if(image.src.match(“bulbon”)){ image.src=“pic_bulboff.gif”; }否则{ image.src=“pic_bulbon.gif”; } }
我只试过点击一次,看看我是否朝着正确的方向前进,即使这样也不行。

试试这个:

<!DOCTYPE html>
<html>
<body>

<img id="myImage" onclick="changeImage()" src="pic_bulboff.gif" width="100" height="180">

<p>Click the light bulb to turn on/off the light.</p>

<script>
// --> Script code should be before closing body tag
function changeImage() {
     alert(100);
    var image = document.getElementById('myImage');
    if (image.src.match("bulbon")) {
        image.src = "pic_bulboff.gif";
    } else {
        image.src = "pic_bulbon.gif";
    }
}
document.getElementById('myImage').click();
</script>

</body>
</html>

单击灯泡以打开/关闭灯光

//-->脚本代码应在结束正文标记之前 函数changeImage(){ 警报(100); var image=document.getElementById('myImage'); if(image.src.match(“bulbon”)){ image.src=“pic_bulboff.gif”; }否则{ image.src=“pic_bulbon.gif”; } } document.getElementById('myImage')。单击();
<!DOCTYPE html>
<html>
<body>

<img id="myImage" onclick="changeImage()" src="pic_bulboff.gif" width="100" height="180">

<p>Click the light bulb to turn on/off the light.</p>

<script>
// --> Script code should be before closing body tag
function changeImage() {
     alert(100);
    var image = document.getElementById('myImage');
    if (image.src.match("bulbon")) {
        image.src = "pic_bulboff.gif";
    } else {
        image.src = "pic_bulbon.gif";
    }
}
document.getElementById('myImage').click();
</script>

</body>
</html>