所以我在 html 文件中使用 JavaScript 脚本编写了一个计数器(使用 Angular 2)。这是我的两个文件(我不包括样式文件)。
pokehunted.component.html
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
<script type="text/javascript" src="../script.js"></script>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div class="displayCounter">
<button class="box" onclick="modify_qty(1)">
<p>Pikachu</p>
<input type="image" src="../../assets/img/pikachu.png" />
</button>
<div class="counter">
<label for="qty"><abbr title="Quantity">Rencontres</abbr></label>
<input id="qty" value="0" />
</div>
</div>
</body>
</html>
脚本.js
function modify_qty(val) {
var qty = document.getElementById('qty').value;
var new_qty = parseInt(qty,10) + val;
if (new_qty < 0) {
new_qty = 0;
}
document.getElementById('qty').value = new_qty;
return new_qty;
}
问题是,我在 Codeacademy 上编写了这个代码,并且运行得很好。目标是创建一个 div 按钮,增加其下方的计数器。在 Codeacademy 上确实如此,而在我的项目中,计数器没有移动。
你知道我做错了什么吗? 非常感谢。
最佳答案
好的,我修好了!我必须将脚本放入 angular-cli.json 中,而不是放入 HTML 中。我真的不知道为什么我不能只使用标签来做到这一点,但它就是这样工作的。这是我在 json 中修改的部分:
"apps": [
{
"scripts": [
"app/pokehunted/script.js"
],
[...]
}
]
关于javascript - 当我的 script.js 在 Codecademy 上运行时无法制作它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46266637/