this和event.target的區別
js中事件是會冒泡的,所以this是可以變化的,但event.target不會變化,它永遠是直接接受事件的目標DOM元素;
this和event.target的相同點
this和event.target都是dom對象,如果要使用jquey中的方法可以將他們轉換爲jquery對象:$(this)和$(event.target);
自己接觸過一個功能(用的是anjular js, ftl):
<tr ng-repeat="item in addWarehouseInventCountRecordModel.recordItemDos">
<td> <input type="text" class="form-control" ng-model="item.countNum" autocomplete="false" required="true" pattern="" name="countNum" οnkeyup="value=value.replace(/[^\d]/g,'') " ng-pattern="/[^a-zA-Z]/" ng-change="changeCountNum(item)"/> </td>
<td> <button type="button" class="btn button-base btn-sm" id="_checkGoodsCodeLoss" ng-click="checkGoodsCodeLoss(item)" ng-disabled=""> 調整商品損益批次 </button> </td> </tr>
其中 ng-model="item.countNum" 輸入框值滿足條件之後,對button按鈕設置disabled屬性
//根據盤點數據計算 $scope.changeCountNum = function (item) { console.log(item); //計算損溢數量 item.profitLossCount = item.countNum - item.inventoryNum; if (item.profitLossCount == 0) { item.purchaseLossStr = 0; item.sellLossStr = 0; $(event.target).parent().parent().children('td').last().find("button").attr("disabled", true); } else { item.purchaseLossStr = ""; item.sellLossStr = ""; $(event.target).parent().parent().children('td').last().find("button").attr("disabled", false); } };之前 $(event.target)這個地方設置爲$(this)獲取不到當前dom節點,留作筆記僅作參考.