【发布时间】:2017-09-07 21:49:24
【问题描述】:
这个标题已经全部问好了,但是我的问题与其他主题不同。
我有一个生成我的 div 的函数:
function createInventory(response)
{
var trHTML = '';
$.each(response, function (i, item) {
//console.log(item.name+": "+item.quantity);
if(item.quantity==null)
{
item.quantity="";
}
trHTML+='<div style="width:24.5%;height: 24.5%;border:1px solid red;float:left;color:white;" id="slot-used'+i+'">'+item.name+"<br>"+item.quantity+'<div class="itemoption"><button id="sell-'+i+'" onclick="sellItem(this.id)" type="button">Sell</button><button id="info'+i+'">Info</button></div></div>';
});
$('.inventory-items').html(trHTML);
}
在html中应该是这样的:
.inventory
{
position: relative;
top:60%;
left:50%;
width:400px;
height:600px;
}
.inventory-items
{
width:100%;
height: 80%;
background: black;
}
.inven-free
{
width:24.5%;
height: 24.5%;
border:1px solid white;
float:left;
}
#slot-used
{
}
#sellquantity
{
width:140px;
height: 80px;
background-color: white;
border:1px solid black;
}
.inventory-head
{
width:100%;
background-color: green;
height: 5%;
position: relative;
}
<div class="inventory">
<div class="inventory-head"><input id=""><button>Sell</button></div>
<div class="inventory-items">
<div style="width:24.5%;height: 24.5%;border:1px solid red;float:left;color:white;" id="slot-used1" class="active">
Soff<br>29
<div class="itemoption"><button id="sell-1" onclick="sellItem(this.id)" type="button">Sell</button><button id="info1">Info</button></div>
</div>
<div style="width:24.5%;height: 24.5%;border:1px solid red;float:left;color:white;" id="slot-used2" class="active">
Clean-water<br>316
<div class="itemoption"><button id="sell-2" onclick="sellItem(this.id)" type="button">Sell</button><button id="info2">Info</button></div>
</div>
<div style="width:24.5%;height: 24.5%;border:1px solid red;float:left;color:white;" id="slot-used3">
Dirty-water<br>127
<div class="itemoption"><button id="sell-3" onclick="sellItem(this.id)" type="button">Sell</button><button id="info3">Info</button></div>
</div>
<div style="width:24.5%;height: 24.5%;border:1px solid red;float:left;color:white;" id="slot-used4">
Saaremaa<br>49
<div class="itemoption"><button id="sell-4" onclick="sellItem(this.id)" type="button">Sell</button><button id="info4">Info</button></div>
</div>
<div style="width:24.5%;height: 24.5%;border:1px solid red;float:left;color:white;" id="slot-used5" class="active">
Glamur<br>6
<div class="itemoption"><button id="sell-5" onclick="sellItem(this.id)" type="button">Sell</button><button id="info5">Info</button></div>
</div>
<div style="width:24.5%;height: 24.5%;border:1px solid red;float:left;color:white;" id="slot-used6">
Laudur<br>25
<div class="itemoption"><button id="sell-6" onclick="sellItem(this.id)" type="button">Sell</button><button id="info6">Info</button></div>
</div>
<div style="width:24.5%;height: 24.5%;border:1px solid red;float:left;color:white;" id="slot-used7">
Lurker<br>
<div class="itemoption"><button id="sell-7" onclick="sellItem(this.id)" type="button">Sell</button><button id="info7">Info</button></div>
</div>
<div style="width:24.5%;height: 24.5%;border:1px solid red;float:left;color:white;" id="slot-used8">
Obi<br>
<div class="itemoption"><button id="sell-8" onclick="sellItem(this.id)" type="button">Sell</button><button id="info8">Info</button></div>
</div>
<div style="width:24.5%;height: 24.5%;border:1px solid red;float:left;color:white;" id="slot-used9">
Savage<br>
<div class="itemoption"><button id="sell-9" onclick="sellItem(this.id)" type="button">Sell</button><button id="info9">Info</button></div>
</div>
<div style="width:24.5%;height: 24.5%;border:1px solid red;float:left;color:white;" id="slot-used10">
Savage<br>
<div class="itemoption"><button id="sell-10" onclick="sellItem(this.id)" type="button">Sell</button><button id="info10">Info</button></div>
</div>
<div style="width:24.5%;height: 24.5%;border:1px solid red;float:left;color:white;" id="slot-used11">
Savage<br>
<div class="itemoption"><button id="sell-11" onclick="sellItem(this.id)" type="button">Sell</button><button id="info11">Info</button></div>
</div>
<div class="inven-free" id="slot-free 0">test</div>
<div class="inven-free" id="slot-free 1">test</div>
<div class="inven-free" id="slot-free 2">test</div>
<div class="inven-free" id="slot-free 3">test</div>
<div class="inven-free" id="slot-free 4">test</div>
</div>
</div>
我正在使用 onclick 来了解我在不同盒子上的 ID 号。
我想将类添加到选定的框(当通过出售按钮选择时)。 现在我的问题是我无法从较旧的点击 id 中删除类。 他们都得到 active 类什么被点击了,但我不想要那个,我只希望被点击的那个有 @987654325 @class,如果点击新的,它应该删除最后一个。
无论如何,我的功能是在这里我如何添加类 atm:
function sellItem(clicked_id)
{
var trHTML = '';
var id=clicked_id.split("-")[1];
$("#slot-used"+id).removeClass('active')
$("#slot-used"+id).addClass('active')
$("#slot-used"+id).toggleClass('active');
trHTML+='<input id=""><button>Sell</button>';
$(".inventory-head").html(trHTML);
}
【问题讨论】:
标签: javascript jquery html css