javascript - $(this) 在我的 jQuery 点击函数中不起作用

当单击我的函数时,我想更改具有此函数元素的父元素。但是 $(this) 不起作用。

怎么了?

function accountConfirm(message, title, yes_label, no_label, callback) {
    console.log($(this));
    $("#accountConfirmModal").attr('data-confirm-yes', false);
    if ($("#accountConfirm").length == 0) {
        .....
    }

    $("#accountConfirmModal .content p").html(message || "");
};

也就是HTML元素

<button type="button" id="deleteScrollTop" class="btn btn-delete"
                                        onclick="accountConfirm(jsResources.addressDeleteQuestion,jsResources.addressDeleteTitle, jsResources.yes,jsResources.no ,function(res){
                                            if(res){
                                                window.location.href = '@(Url.RouteUrl("CustomerAddressDelete", new {addressId = address.Id}))'
                                            }
                                        })">
                                    <img src="@Url.ThemeFolderUrl("_images/delete.png")" alt="delete address" />
                                </button>

最佳答案

你可能打算这样做

如果您使用 jQuery 而不是使用内联事件处理程序,那么合并 jQuery 总是一个好主意

假设按钮ID是唯一的

function accountConfirm(message, title, yes_label, no_label, callback) {
  $("#accountConfirmModal").attr('data-confirm-yes', false);
  if ($("#accountConfirm").length == 0) {
    .....
  }

  $("#accountConfirmModal .content p").html(message || "");
};

const route = function(res) {
  if (res) {
    window.location.href = '@(Url.RouteUrl("CustomerAddressDelete", new {addressId = address.Id}))'
  }
}
$("#deleteScrollTop").on("click", function() {
  console.log($(this));
  accountConfirm(jsResources.addressDeleteQuestion,
    jsResources.addressDeleteTitle,
    jsResources.yes,
    jsResources.no, route)
})
<button type="button" id="deleteScrollTop" class="btn btn-delete">
  <img src="@Url.ThemeFolderUrl("_images/delete.png")" alt="delete address" />
</button>

https://stackoverflow.com/questions/69084548/

相关文章:

node.js - 如何在 nodejs 中使用 getRandomValues()?

go - 在 golang 中将 []*string 转换为 []string

python - 您如何为加权平均平均值迭代地为数据框列赋予权重?

snowflake-cloud-data-platform - 使用 Google 作为身份提供商

c++ - 如何区分ascii值和数字?

swiftui - PresentationMode 触发器 "Variable X used be

javascript - JS : proper way of using optional cha

git - 如何创建自定义 git/bash 脚本?

c# - 如何按值列表分组,然后计算每个值的条目数量

ruby-on-rails - Rubocop 在 Rails 中的蓝图序列化程序自定义字段中警告