事件监听器问题

时间:2018-08-14 19:05:07

标签: javascript jquery

我正在学习事件侦听器,但单击图像时无法使其添加一行文本 我的代码:

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Cat Clicker</title>
    <link rel="stylesheet" type="text/css" href="css/styles.css">
    <script 
src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"> 
</script>
</head>
<body>

    <img alt= "Black Cat" src= "img/tesla-cat.jpg" class="gatito">
    <p class="clicks"> </p>
    <script src="js/app.js"></script>
    </body>
</html>

这是javascript文件:

$(".gatito").click(function() {
    var text = "";
    var i;
    for (i = 0; i<0; i++) {
    text += ("The kitten has been clicked " + i + "times");

}

$(".clicks").innerHTML= text;

}

);

伸出援手?

1 个答案:

答案 0 :(得分:0)

请检查代码。您的for循环永远不会运行,因为终止条件始终为false,即i < 0始终为false。

$(".gatito").click(function(){
    var text = "";
    var i;
    var countLimit = 5;//some value greater than 0
    for (i = 0; i < countLimit; i++) {
      text += ("The kitten has been clicked " + i + "times");
    }
    $(".clicks").text(text);
 }

);