jQuery中 this与event.target区别

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节点,留作笔记仅作参考.



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章