如何在html中为图像分配数字
How to assign a number to an image in html?
要清楚,我如何将图像与数字相关联?我正在尝试使用javascript制作一个"多少手指游戏"。我将用图像来回答。每个数字都有对应的图像。(例如,用一根手指表示答案1的图片。)当其中一张图片被点击时,程序将检查随机数(0、1、2、3、4或5)是否等于图片中的手指数。
我猜img标签不能得到一个值属性。我试着给每个图像一个值,并根据这个值设置一个if语句,但它不起作用。
它总是给出错误作为输出。jsfiddle: http://jsfiddle.net/4g0weo75/
<!doctype html>
<html>
<head>
<title>Java script</title>
<meta charset="utf-8" />
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<style type="text/css">
.break{
clear:both;
}
#answer{
float:left;
}
#myButton{
float:left;
}
.fingers{
float:left;
display:block;
padding-right:50px;
}
.fingers img{
border:1px solid black;
border-radius:20px;
}
</style>
</head>
<body>
<img width="100px" height="auto" src="http://i62.tinypic.com/2rr55xv_th.jpg" />
<p>How many fingers am I holding up?</p>
<div class="break"></div>
<p class="fingers"><img id="zero" src="http://i61.tinypic.com/140jfnp_th.png" value="0" /></p>
<p class="fingers"><img id="one" src="http://i61.tinypic.com/6tfgpv_th.png" value="1" /></p>
<p class="fingers"><img id="two" src="http://i62.tinypic.com/2w685ya_th.png" value="2" /></a></p>
<p class="fingers"><img id="three" src="http://i58.tinypic.com/hsrksj_th.png" value="3" /></a></p>
<p class="fingers"><img id="four" src="http://i57.tinypic.com/igdfrc_th.png" value="4" /></a></p>
<p class="fingers"><img id="five" src="http://i60.tinypic.com/2useki1_th.png" value="5" /></a></p>
<script type="text/javascript">
document.getElementById("zero").onclick=function(){
var x=Math.random(x);
x=6*x;
x=Math.floor(x);
if (x==document.getElementById("zero").value){
alert ("You got it!!");
}else {
alert ("You're wrong. The number was: "+x);
}
}
document.getElementById("one").onclick=function(){
var x=Math.random(x);
x=6*x;
x=Math.floor(x);
if (x==document.getElementById("one").value){
alert ("You got it!!");
}else {
alert ("You're wrong. The number was: "+x);
}
}
document.getElementById("two").onclick=function(){
var x=Math.random(x);
x=6*x;
x=Math.floor(x);
if (x==document.getElementById("two").value){
alert ("You got it!!");
}else {
alert ("You're wrong. The number was: "+x);
}
}
document.getElementById("three").onclick=function(){
var x=Math.random(x);
x=6*x;
x=Math.floor(x);
if (x==document.getElementById("three").value){
alert ("You got it!!");
}else {
alert ("You're wrong. The number was: "+x);
}
}
document.getElementById("four").onclick=function(){
var x=Math.random(x);
x=6*x;
x=Math.floor(x);
if (x==document.getElementById("four").value){
alert ("You got it!!");
}else {
alert ("You're wrong. The number was: "+x);
}
}
document.getElementById("five").onclick=function(){
var x=Math.random(x);
x=6*x;
x=Math.floor(x);
if (x==document.getElementById("five").value){
alert ("You got it!!");
}else {
alert ("You're wrong. The number was: "+x);
}
}
</script>
</body>
</html>
<!-- end snippet -->
你应该尝试使用html5中可用的data属性,你可以将代码更改为:
<p class="fingers"><img id="zero" src="http://i61.tinypic.com/140jfnp_th.png" data-value="0" /></p>
<p class="fingers"><img id="one" src="http://i61.tinypic.com/6tfgpv_th.png" data-value="1" /></p>
<p class="fingers"><img id="two" src="http://i62.tinypic.com/2w685ya_th.png" data-value="2" /></a></p>
<p class="fingers"><img id="three" src="http://i58.tinypic.com/hsrksj_th.png" data-value="3" /></a></p>
<p class="fingers"><img id="four" src="http://i57.tinypic.com/igdfrc_th.png" data-value="4" /></a></p>
<p class="fingers"><img id="five" src="http://i60.tinypic.com/2useki1_th.png" data-value="5" /></a></p>
那么您可以使用以下行:
document.getElementById("zero").getAttribute('data-value')
在这种情况下,value
是标签的属性,因此document.getElementById("two").value
使用document.getElementById("two").getAttribute('value')
代替。您可以使用document.getElementById('some_form_element_id').value
与表单元素-图像不是一个表单元素。
相关文章:
- 使用 HTML 输出标记查看可缩放图像并分配 src
- 多个图像显示带有动态创建的画布的最后一个图像并分配给图像
- 使用 JQuery 从另一个元素的 id 为图像分配属性
- 将base64图像数据作为src分配给图像时,Firefox和Chrome上的结果不一致
- HTML服务为图像分配URL
- 如何分配在画布中绘制的图像的点击
- 如何使用在 javascript 中分配给图像的变量,并使用 switch 语句在 html 中显示它们
- JS循环图像文件夹,预加载它们并分配给数组
- 严格模式下的图像数据数据分配
- 将多个图像(文件)从一个文件输入分配给另一个文件输入 - 上传
- 将类分配给第一个图像
- 如何在 HTML 文件中分配图像
- 随机为图像分配一个值,然后传递动画
- 是否可以加载图像,在内存中缩放它并将其分配为背景图像的 uri
- 根据单元格的值将CSS生成的图像分配给表中的单元格
- 不能为动态创建的图像分配样式
- 如何在html中为图像分配数字
- 如何使用jquery-css将按钮的背景图像分配给变量
- 在动态情况下为相应的图像分配按钮
- 为不同的图像分配不同的链接,并让它在iframe容器上播放指定的(youtube)链接