1

我有一个图像地图,想跟踪它在 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>

上面是图像映射,下面是 javascript 和带有计数 ID 的段落。

<script type="text/javascript">

    var count;

    document.getElementById("counted").innerHTML = "This has been clicked " + count + " times.";

</script>

<p id="counted"></p>

每次单击图像映射时,如何增加变量计数?

4

4 回答 4

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>
于 2012-07-04T15:47:44.077 回答
0

给你:http: //jsfiddle.net/PUs9g/

于 2012-07-04T15:47:56.000 回答
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 添加到地图本身。但是,如果您要计算特定区域,请从那里调用它。

于 2012-07-04T15:49:10.490 回答
0

如果您在没有关键字 var 的情况下定义计数器,则它是一个全局变量。

你只需要改变

var count;

count;
于 2012-07-04T15:49:55.737 回答