我如何使用Javascript OOP制作一个循环直到玩家击败敌人?
var character = {};
character.health = 5;
character.damage = 1;
var npc = {};
npc.health = 5;
do {
npc.health = npc.health -= character.damage;
document.write(npc.health + "<br>");
}
while (npc.health != 0);
if (npc.health == 0) {
document.write("Player wins.");
}
Run Code Online (Sandbox Code Playgroud)
当角色伤害设置为1时可以正常工作,但一旦它超过1,它就会永远循环并且浏览器会崩溃吗?我糊涂了.另外我如何让它显示0而不是像-1等负数?
我的代码抛出JS错误,offsetParent未设置 - 无法滚动.我试过position: relative;但它仍然显示相同的错误.
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.9/jquery-ui.js" type="text/javascript"></script>
<link href="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.9/themes/blitzer/jquery-ui.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
$(document).ready(function() {
$('#btnShow').click(function(){
$("#dialog").dialog();
});
});
</script>
<a href="#" id="trigger">this link</a>
<div id="dialog" style="display: none; position: relative; height: 4em; overflow: scroll;">
<div>
<iframe src="reports/my_pdf.pdf"></iframe>
</div>
</div>
Run Code Online (Sandbox Code Playgroud) 这是我的代码:
$(".cbtn").click(function(e) {
alert("here");
var id = $(this).attr('id');
var comment = $('input[id$=id]').val();
alert("hay" + id);
alert(comment);
$.post("comments.php", {
id: id,
comment: comment
})
});
});
Run Code Online (Sandbox Code Playgroud)
的idof cbtn和文本字段是动态创建的,如下所示:
<input type="text" class="enter_comment value="comments" id="comm<?php echo $row['p_id'];?>"/>
<input type="button" value="enter" class="cbtn" id="<?php echo $row['p_id'];>">
Run Code Online (Sandbox Code Playgroud)
当cbtn被点击时,我想要得到的id文本字段具有相同ID的目的cbtn。但是它正在返回undefined。
我只能得到input标签,但我也希望得到textarea标签.我怎样才能做到这一点?
<div class="common-div">
<input type="text" name="confirm-pwd"/>
<textarea cols="150" rows="5"></textarea>
</div>
<script>
$(".common-div input").each(function() {}):
</script>
Run Code Online (Sandbox Code Playgroud) 如何从id = 2JavaScript/jQuery中的以下数组中删除整个子数组项?
arr = [{
"id": 1,
"name": "Steve"
},{
"id": 2,
"name": "Martin"
},{
"id": 3,
"name": "Short"
}]
Run Code Online (Sandbox Code Playgroud)
我不确定如何使用grep或splice在这种情况下.
我想要的输出是:
newArr = [{
"id": 1,
"name": "Steve"
},{
"id": 3,
"name": "Short"
}]
Run Code Online (Sandbox Code Playgroud) 我目前有一些ASP.Net代码,它构建一个输出,然后通过字符串文字显示它.我们的想法是,每个人Receipt都有一个"查看更多"按钮,可以切换display: none;要开始的额外信息.我试图用这个eq()方法试图找到我想要切换的那个,因为我在里面做了ul.我目前的代码是这样的:
$("#btn-expand").click(function () {
var ldx = $("#list li .active").index(); // Get the list number so we know what ID to work with
var idx = $("#expand").next().index(); // Get the next ID index point
// Check that it isn't going negative
if (idx == -1 || ldx == -1) {
// If it is, reset ldx
ldx = 0;
idx = 0;
}
$("#expand").eq(ldx).toggle(); // Toggle that one
console.log(ldx);
});
Run Code Online (Sandbox Code Playgroud)
第一个按钮工作正常,然后 …
我有以下表结构:
<tr class="tabrow" val="12345">
<td class="clickable">abcde</td>
<td class="clickable">fghij</td>
<td class="clickable">2016-03-03 15:35:45</td>
</tr>
Run Code Online (Sandbox Code Playgroud)
val当我单击任何字段时,我想要一种获取行的方法。到目前为止,我已经知道了:
$('.clickable').on('click', function() {
evID = $(this).closest('tr').val();
alert(evID);
})
Run Code Online (Sandbox Code Playgroud)
这行不通;它只是返回一个空白结果。我也试过了,这也行不通:
evID = $(this).parent().val();
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
这是我的HTML文件的一部分;
<script type="text/template" id="_profile">
<div class="modalDialog">
<div class="profile">
<div class="box clearfix">
<div class="box-header clearfix">
<span class="box-title l">{% trans 'PROFILE' %}</span>
<a href="#" style="margin-left: 10px;" id="profile_export" class="l"><span class="icon-download"></span>{% trans 'Profile Export' %}</a>
<div class="close-profile icon-cancel-circled r"></div>
</div>
Run Code Online (Sandbox Code Playgroud)
和
$('.profile').on('click', '#profile_export', function (e) {
e.preventDefault();
$.get(root + '/profile/export/', function(resp) {
// DO something
Run Code Online (Sandbox Code Playgroud)
我已经看了一些"jQuery中的事件委托"的例子,所以我写了上面的函数.可能是错的,因为当我点击它时,它什么也没做.它只是放在#当前URL的末尾.
谁能告诉我这里有什么问题?为什么当我点击#profile_export它不会触发"profile/export /"的请求?
谢谢
我正面临着一个问题on().这是我的代码:
<span class="add" id="id_foo" >add this stuff</span>
Run Code Online (Sandbox Code Playgroud)
$(".add").on('click', function() {
$.post("/toto.com/add.php", { trainning: this.id }, function() {});
$(this).html("Remove this stuff");
$(this).removeClass("add").addClass("remove");
//alert($(this).attr("class"));-->give the good class
});
$(".remove").on('click', function() {
//doesn't work
$.post("/toto.com/remove.php", { trainning: this.id }, function() {});
$(this).html("add this stuff");
$(this).removeClass("remove").addClass("add");
});
Run Code Online (Sandbox Code Playgroud)
类的选择器remove无法识别它,而当我单击带有add类的按钮时,类正在改变.你知道问题是什么吗?
谢谢您的帮助!
您好,我在表中有一个带有值的 td,当单击按钮时,我已将此 td 更改为文本类型的输入。
现在我想用 td 元素的 id 更改新输入的 id,所以我有一个例外:
input.id 不是函数
这是我的代码js:
$("tr.forc td.TdbeforeForcage").each(function () {
var html = $(this).html();
var input = $('<input class="numberforce" style="width:50%" type="text" />');
var IdTd = $(this).attr("id");
input.val(html);
input.id(IdTd); // Here trying to change id of input with id of td
$(this).html(input);
if (html.indexOf(' ') > -1) {
var newValue = html.replace(' ', ' ');
$(this).find('input.numberforce').val(newValue)
}
});
Run Code Online (Sandbox Code Playgroud)