簡體   English   中英

使用javascript單擊html按鈕

[英]Click a html button using javascript

我必須單擊TWICE的html按鈕來實現我的項目中需要的功能。因此,我正在使用javascript使用click()單擊按鈕。但是以下腳本對我不起作用。 請在此處嘗試一下http://www.w3schools.com/js/tryit.asp?filename=tryjs_lightbulb

<!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>document.getElementById('myImage').click();
        document.getElementById('myImage').click();
  </script>


</body>

<script>
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";
    }
}
</script>

</html>

我試過只單擊一次,只是看看我是否朝着正確的方向前進,即使那行不通。

嘗試這個:

<!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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM