我有一个imagemap,想要跟踪它在javascript中被点击的次数,但我不知道如何记录点击次数。
<p>
<img src="image.png" alt="missing" width="396" height="376" border="0" usemap="#thatPageMap" />
<map name="imageMap" id="thatPageMap">
<area shape="poly" coords="0,376,-3,269,50,251,60,234,71,225,76,192,69,178,44,127,50,82,73,62,94,48,69,46,108,30,145,20,162,29,162,16,183,34,193,41,228,35,247,38,225,47,242,47,279,53,306,78,324,117,326,150,335,179,327,200,327,221,351,234,357,263,393,275,394,378" href="thatpage.com" alt="Refresh" />
</map>
</p>
上面是imagemap,下面是javascript和带有计数ID的段落。
<script type="text/javascript">
var count;
document.getElementById("counted").innerHTML = "This has been clicked " + count + " times.";
</script>
<p id="counted"></p>
每次点击图像地图时如何增加变量计数?
答案 0 :(得分:3)
例如:
<script type="text/javascript">
var count = 0;
function updateCounter( ) {
count++;
document.getElementById("counted").innerHTML = "This has been clicked " + count + " times.";
}
</script>
<p id="counted"></p>
<map name="imageMap" id="thatPageMap" onclick="updateCounter()">
<area shape="poly" coords="0,376,-3,269,50,251,60,234,71,225,76,192,69,178,44,127,50,82,73,62,94,48,69,46,108,30,145,20,162,29,162,16,183,34,193,41,228,35,247,38,225,47,242,47,279,53,306,78,324,117,326,150,335,179,327,200,327,221,351,234,357,263,393,275,394,378" href="thatpage.com" alt="Refresh" />
</map>
答案 1 :(得分:0)
答案 2 :(得分:0)
创建一个函数来增加点击次数,然后通过区域上的onclick调用它。
<script type="text/javascript">
var count;
function clicked() {
count = count + 1;
document.getElementById("counted").innerHTML = "This has been clicked " + count + " times.";
}
</script>
<p>
<img src="image.png" alt="missing" width="396" height="376" border="0" usemap="#thatPageMap" />
<map name="imageMap" id="thatPageMap">
<area onclick="clicked();" shape="poly" coords="0,376,-3,269,50,251,60,234,71,225,76,192,69,178,44,127,50,82,73,62,94,48,69,46,108,30,145,20,162,29,162,16,183,34,193,41,228,35,247,38,225,47,242,47,279,53,306,78,324,117,326,150,335,179,327,200,327,221,351,234,357,263,393,275,394,378" href="thatpage.com" alt="Refresh" />
</map>
</p>
或者将onclick添加到地图本身。但是如果你在计算特定区域,请从那里调用它。
答案 3 :(得分:0)
如果您定义的计数器没有关键字var,则它是一个全局变量。
你只需要改变
var count;
到
count;