使用JavaScript if/else函数返回图像

使用JavaScript if/else函数返回图像,javascript,jquery,Javascript,Jquery,所以我尝试使用JavaScript函数返回一个图像。我希望用户输入“艺术”或“旅行”,然后根据他们的输入返回两个图像中的一个。我不是一个程序员,这是我试图为我的一门基础技术课做的事情,所以我真的是个笨蛋,老实说,我不懂我在其他答案中读到的一些行话。如果有人能简单地说明我做错了什么,我会非常感激。这就是我写的代码:它一直告诉我返回部分有一个未捕获的SyntaxError,它告诉我我的函数没有定义 谢谢 <h3>Please indicate whether you would like

所以我尝试使用JavaScript函数返回一个图像。我希望用户输入“艺术”或“旅行”,然后根据他们的输入返回两个图像中的一个。我不是一个程序员,这是我试图为我的一门基础技术课做的事情,所以我真的是个笨蛋,老实说,我不懂我在其他答案中读到的一些行话。如果有人能简单地说明我做错了什么,我会非常感激。这就是我写的代码:它一直告诉我返回部分有一个未捕获的SyntaxError,它告诉我我的函数没有定义

谢谢

<h3>Please indicate whether you would like to see a collage of art that I have made, or a collage of pictures made of places I have visited (please type "Art" or "Travel"):</h3>

<input id="answer"> <!---start of first function (html)-->

<button type="button" onclick="myFunction()">Submit</button>

<!--end of first function (html)--> 

<script> <!---start of first function (javascript)--> 
function myFunction() {
    var x, text;

    x = document.getElementById("answer").value;

    if (x == Art) { 
        return <a href="http://imgur.com/21cHFIU"><img src="http://i.imgur.com/21cHFIU.png" title="source: imgur.com" /></a>
    }else  {
        return <a href="http://imgur.com/3UFIhPG"><img src="http://i.imgur.com/3UFIhPG.png" title="source: imgur.com" /></a>

    }


}
</script> <!---end of first function (javascript)-->
请说明您是想看我制作的艺术拼贴,还是想看我参观过的地方制作的图片拼贴(请键入“艺术”或“旅行”):
提交
函数myFunction(){
变量x,文本;
x=document.getElementById(“应答”).value;
如果(x==艺术){
返回
}否则{
返回
}
}

您需要返回一个字符串,然后进行渲染,您当前返回的语法无效

另一个选项是在标记中包含img标记,并在提交按钮时更改src属性

<h3>Please indicate whether you would like to see a collage of art that I have made, or a collage of pictures made of places I have visited (please type "Art" or "Travel"):</h3>

<input id="answer"> <!---start of first function (html)-->

<button type="button" onclick="myFunction()">Submit</button>
<img id="image" src="" title="source: imgur.com" />
<!--end of first function (html)--> 

<script> <!---start of first function (javascript)--> 
function myFunction() {
    var image = document.getElementById("image");
    var answer = document.getElementById("answer").value;

    if (answer === "Art") { 
        image.src = "http://imgur.com/21cHFIU";
    } else if (answer === "Travel") {
        image.src = "http://imgur.com/3UFIhPG";
    }
}
</script> <!---end of first function (javascript)-->
请说明您是想看我制作的艺术拼贴,还是想看我参观过的地方制作的图片拼贴(请键入“艺术”或“旅行”):
提交
函数myFunction(){
var image=document.getElementById(“image”);
var answer=document.getElementById(“answer”).value;
如果(答案==“艺术”){
image.src=”http://imgur.com/21cHFIU";
}否则如果(回答==“旅行”){
image.src=”http://imgur.com/3UFIhPG";
}
}

下面的注释不是Javascript注释语法:

在脚本标记中,必须使用如下注释:

//第一个函数的开始(javascript)

而不是:

if(x==Art)

您必须使用引号,因为
Art
是字符串,而不是Javascript关键字。改为这样做:

if(x==“Art”)

您将在此处返回一个非Javascript变量:

返回

所以你必须使用这样的引号:

return'

这应该可以

<h3>Please indicate whether you would like to see a collage of art that I have made, or a collage of pictures made of places I have visited (please type "Art" or "Travel"):</h3>
<input id="answer">
<button type="button" onclick="myFunction()">Submit</button>
<img id="image" src="" />

<script>
function myFunction() {
    var x = document.getElementById("answer").value;
    var image = document.getElementById("image");

    if (x == 'Art') { 
        image.src = 'http://i.imgur.com/21cHFIU.png';
    }else  {
        image.src = 'http://i.imgur.com/3UFIhPG.png';
    }
}
</script> 
请说明您是想看我制作的艺术拼贴,还是想看我参观过的地方制作的图片拼贴(请键入“艺术”或“旅行”):
提交
函数myFunction(){
var x=document.getElementById(“应答”).value;
var image=document.getElementById(“image”);
如果(x=='Art'){
image.src=http://i.imgur.com/21cHFIU.png';
}否则{
image.src=http://i.imgur.com/3UFIhPG.png';
}
}

好的,让我们试着向您解释一下您的代码出了什么问题

1-在这里,您应该将
字符串
与文本进行比较。所以你需要用引号来说明艺术是文本:
“艺术”

if (x == Art) {
2-不应将文本返回到按钮事件。也就是说,您试图从此
函数返回的
字符串存在一些问题。这就是您得到的
未损坏SyntaxError
的原因。因此,正确的方法是:

'<a href="http://imgur.com/21cHFIU"><img src="http://i.imgur.com/21cHFIU.png" title="source: imgur.com" /></a>'
请说明您是想看我制作的艺术拼贴,还是想看我参观过的地方制作的图片拼贴(请键入“艺术”或“旅行”):
提交




我想你的意思是
如果(x=='Art')
。。。并
返回“”
返回同样,return语句应该返回一个字符串。单击按钮时,您可以提供更多关于所需结果的信息吗?