0

我有 3 个链接图像,您应该单击它们,然后下方会出现一个标题,还有一个可单击的按钮,它将带您到该站点。所有 3 个都通过相同的按钮和标题,因此必须根据您单击的图像进行更改。我只被允许使用一个函数来完成这项工作。所以我这样做了,但是当我单击按钮时它仍然没有做任何事情。

我不允许对结构造成太多干扰 - 我必须获得某种功能,以使所有 3 个链接图像根据单击的图像输出 3 个变量(标题、按钮和目标)。我已经让它与 3 个不同的功能一起工作,但现在我必须用一个来做。

我是个新手,所以请告诉我如何尽可能简单地做到这一点。谢谢你!

<html>
<head>
<script>
var caption1='My Blog';
var button1='My Blog';
var target1= 'http://iamunicorngirl.livejournal.com';

var caption2='Nail Polish Line';
var button2='Nail Polish Line';
var target2='http://mustachelacquer.etsy.com';

var caption3='Failblog';
var button3='Failblog';
var target3='http://failblog.org';

function ButtonFunction(caption1, button1, target3);
{
document.getElementById('linkbox').innerHTML=caption1;
document.getElementById('mybutton').innerHTML=button1;
document.getElementById('linkbox').setAttribute('onclick','window.location.href="'+ target3 +'";');
}
</script>
</head>

<table>
<table align="center">
<tr>
<td><img type="picture" src="blog.png" onclick="ButtonFunction(caption1, button1, target1)";></td>
<td><img type="picture" src="nailpolish.png" onclick="ButtonFunction(caption2, button2, target2)";></td>
<td><img type="picture" src="failblog.png" onclick="ButtonFunction(caption3, button3, target3)";></td>
</tr>
<tr>
<td><h3 type="text" id="linkbox"></h3></td>
</tr>
<tr>
<td><button id="mybutton"></button></td>
</tr>
</table>
</html>
4

1 回答 1

0

我注意到您提供的代码存在两个问题:

1) 您的ButtonFunction. 它应该是这样的:

function ButtonFunction(caption1, button1, target3) { ...

2)您的分号超出了您的onclick定义。它们应该是这样的:

... onclick="ButtonFunction(caption1, button1, target1);"> ...

工作示例

请注意,在浏览器中使用开发者控制台会很有帮助。Javascript 错误变得容易识别。例如:SyntaxError: Expected token '{'

于 2013-10-03T19:14:50.473 回答