jquery函数';s的运行时间很慢
这个函数运行得很好,但有点慢 htmljquery函数';s的运行时间很慢,jquery,runtime,Jquery,Runtime,这个函数运行得很好,但有点慢 html <table> <tr> <td> <p><span id="ItemPrice">10</span></p> </td> <td> <input type="number" id="quantity" name="quantity" val
<table>
<tr>
<td>
<p><span id="ItemPrice">10</span></p>
</td>
<td>
<input type="number" id="quantity" name="quantity" value="1" min="1" />
</td>
<td>
<p><span id="ItemTotal">0</span></p>
</td>
</tr>
</table>
你知道如何增加这个函数的运行时间吗
“我不知道为什么我收到了这条消息,但它说我必须添加一些额外的文本,因为我的大部分帖子都是代码,不管怎样,请不要介意:)”为了让您的计算性能更好,您可以进行以下更改
.fadeOut(“fast”)
和.fadeIn(“fast”)
id
s更改为class
es,因为您提到需要多次重复这些行each()
,以便只更新当前行函数CountItemTotal($this){
var tr=$this.closest(“tr”);
var ItemTotal=tr.find(“.ItemTotal”);
var ItemPrice=tr.find(“.ItemPrice”);
var数量=tr.find(“.quantity”);
itemtottal.text(ItemPrice.text()*quantity.val());
}
$(“input.quantity”).change(函数(){
CountItemTotal($(本));
});代码>
十,
0
十,
0
十,
0
十,
0
我得到的只是帮助
这个查询的效果会比我发布的第一个更好
function CountItemTotal($this) {
var tr = $this.closest("tr");
var ItemTotal = tr.find(".ItemTotal");
var ItemPrice = tr.find(".ItemPrice");
var quantity = tr.find(".quantity");
ItemTotal.fadeOut(function () {
$(this).text(ItemPrice.text() * quantity.val()).fadeIn();
});
}
$("input.quantity").change(function() {
CountItemTotal($(this));
});
元素淡出然后以新的值出现:)从代码中删除。.fadeOut(“fast”).fadeIn(“fast”)
。@user9263373是否有其他方法来实现效果?我不知道CountItemTotal()
将在每次更改时应用该效果。除非有人知道如何只应用一次效果,否则您必须删除效果。另一种加快代码速度的方法是通过删除。each()
,最近的()
和查找()
。如果我删除了。each()
,最近的()
和查找()
整个代码将毫无用处,因为实际代码中有很多行,我需要找到每行的具体价格和总价!!好的,这样做很好,比我的更清楚谢谢你的帮助我很抱歉我有11个名声,所以我不能投你的票answer@AhmedMohamed别担心。很高兴它能帮助你。
function CountItemTotal($this) {
var tr = $this.closest("tr");
var ItemTotal = tr.find(".ItemTotal");
var ItemPrice = tr.find(".ItemPrice");
var quantity = tr.find(".quantity");
ItemTotal.fadeOut(function () {
$(this).text(ItemPrice.text() * quantity.val()).fadeIn();
});
}
$("input.quantity").change(function() {
CountItemTotal($(this));
});