我目前正在开展一个非常愚蠢的项目,只是为了进入jQuery的大门。这是我到目前为止所得到的:
$("#buyOne").click(function() {
one++;
var costOne = 10;
if (coins - costOne < 0) {
alert("Dogs ain't free, ya know!");
} else {
dogs += 1;
coins -= costOne;
$("#main").html("You have " + coins + " coins.");
var levelOne = $("<div></div>", {
"class" : "levelOne dog one"+one
})
$("body").append(levelOne);
var battleOne = $("<button class='battleOne'>Send dog to battle</button>");
$(".one"+one).append(battleOne);
}
});
$(".battleOne").click(function() {
coins += 1;
alert("Your dog won the battle! + 1 coin");
});
对于时髦的StackOverflow格式感到抱歉,但我的问题是.battleOne
上的点击功能不起作用。谁能告诉我我做错了什么?谢谢!
答案 0 :(得分:2)
<HTML>
<script src="js/main.js"></script>
//for converting document.getelementbyid() to _()
<body>
<div id="form_place">
<form>
<input name="nos" id='nos' type="text">
<button onclick="addnos()">Enter</button>
</form>
</div>
</body>
<SCRIPT>
function addnos(){
var nos = _('nos').value;
for (i=0;i < nos;i++){
manddy = document.createElement('INPUT');
manddy.setAttribute("id", 'name'+i+1);
_('form_place').appendChild(manddy);
}
}
</SCRIPT>
</HTML>
&#13;
var battleOne = $("<button class='battleOne'>Send dog to battle</button>");
$(".one").click(function() {
$('body').append(battleOne);
});
$(document).on("click",".battleOne",function() {
alert("Your dog won the battle! + 1 coin");
});
&#13;