简体   繁体   English

JavaScript-为什么代码不起作用?

[英]JavaScript - why the code doesn't work?

I've been learning JavaScript from tutorials online but I couldn't get these two tasks work.I can't see what's wrong in my code.... 我一直在从在线教程中学习JavaScript,但无法完成这两个任务,看不到我的代码有什么问题....

This is intended to change a image into another when viewer clicks on it. 目的是在查看者单击图像时将其更改为另一个图像。

<script>
    function LightUp(){
        element = document.getElementById(myimage)
        if(element.src.match("eg_bulboff.gif")){
            element.src = "eg_bulbon.gif";
        }else{
            element.src = "eg_bulboff.gif";
        }
    }
</script>
<p>Click the bulb to light it up!</p>
<p><img id="myimage" onclick="LightUp()" src="eg_bulboff.gif"></p>

The code below is intended to change the text into red 以下代码旨在将文本更改为红色

<p id="demo4">Change my colour</p>
<script>
    function Red(){
        x = document.getElementById("demo4")
        x.stype.color = "#ff0000";
    }
</script>
<button type="button" onclick="Red()">Click me</button>

Thanks in advance! 提前致谢!

You need to make myimage a string. 您需要将myimage为字符串。

element = document.getElementById('myimage');

You should also consider declaring your variables with var or they will end up being global variables. 您还应该考虑使用var声明变量,否则它们最终将成为全局变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM