gpt4 book ai didi

javascript - 在 javascript/jQuery 中调用函数作为函数

转载 作者:行者123 更新时间:2023-12-02 18:36:46 24 4
gpt4 key购买 nike

我想知道下面重复或给这个“函数”一个名称的最快方法是什么,以便我可以在同一文档中的另一个函数中调用它 -> runThisFunction();

代码:

$('body').on("click", ".manufacturer_details_link", function (e) {
e.preventDefault();

var self = $(this);
var id = self.data("id");

var url = $("#manufacturers_table").data("infourl");

var data_array = {
id : id
};

ajaxCall(url, data_array, "rhs_info");

});

最佳答案

如果我理解一切正确:

function bodyOnClick( e, optionalId ) {
var nodeRef = optionalId ? document.getElementById( optionalId ) : this;

if( typeof e === 'object' )
e.preventDefault();

var self = $(this);
var id = nodeRef.data("id");

var url = $("#manufacturers_table").data("infourl");

var data_array = {
id : id
};

ajaxCall(url, data_array, "rhs_info");
}

// ---

$('body').on("click", ".manufacturer_details_link", bodyOnClick );

// ---

bodyOnClick( 'someNodeID' );

但实际上,调用 bodyOnClick plain 没有多大意义,因为 this不会引用任何 HTMLElement。因此,只有在其他事件处理程序上重复使用该方法,或者像

那样显式调用它才有意义
bodyOnClick.call( someNodeReference );

<罢工>

关于javascript - 在 javascript/jQuery 中调用函数作为函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17246195/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com