我正在制作一个简单的应用程序,可以在点击时增加每张图片下方的计数。我认为它在编译时遇到问题,或者我遗漏了一些愚蠢而明显的东西。
这是我的 HTML 和 JS
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Cat Clicker</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src='app.js'></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.1/jquery-ui.min.js"></script>
</head>
<body>
<h1> Cat Clicker </h1>
<img class="catpic_one" src="cat.jpg" alt="Mountain View" style="width:304px;height:228px">
<p class="counter_one"> 0 </p>
<img class="catpic_two" src="catpic2.jpg" alt="Mountain View" style="width:304px;height:228px">
<p class="counter_two"> 0 </p>
</body>
</html>
JS
$(document).ready(function() {
var clicks_one = 0;
var clicks_two = 0;
$('#catpic_one').on("click", "#counter_one", function() {
clicks_one++
var clickcount_one = clicks_one.toString();
$('#counter_one').append("You clicked the cat #1" clickcount_one " times.");
}
$('#catpic_two').on("click", "#counter_two", function() {
clicks_two++
var clickcount_two = clicks_two.toString();
$('#counter_two').append("You clicked the cat #1" clickcount_two " times.");
}
});
最佳答案
catpic_one 和 catpic_two 是类,不是 id。请将 '#' 更改为 '.'
,如
'.catpic_one' and '.catpic_two'
关于javascript - 我在这里把问题括起来了吗?或者真的有一个意想不到的标识符? Javascript/jQuery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29083701/