gpt4 book ai didi

javascript - jQuery 或 JavaScript 使用元素中的嵌入字符串作为函数引用

转载 作者:行者123 更新时间:2023-11-28 20:40:34 25 4
gpt4 key购买 nike

是否有可能有一个链接,我的链接看起来像这样

<a href="something/" class="something" data-callback="thisFunction">
<a href="something/" class="something" data-callback="thatFunction">
<a href="something/" class="something" data-callback="anotherFunction">
<a href="something/" class="something" data-callback="">

<script>
$('.something').click(function(e)
{
e.preventDefault();
var cb = $(this).data('callback');
if(cb !== undefined && cb !== null && cb !== '')
{
//somehow use cb as a means to call another function
//similar in effect to calling "thatFunction();"
}
});
function thisFunction()
{
//code
}
function thatFunction()
{
//code
}
function anotherFunction()
{
//code
}
</script>

如果可能的话,我将如何实现使其工作,以及该类型调用的实际引用是什么,例如执行此操作所调用的方法是什么,所以将来如果我忘记了,希望我会记住什么该方法被调用,我可以直接查找。

最佳答案

您可以使用以下内容:

window[cb]();

或者为了安全起见:

typeof window[cb] === "function" && window[cb]();

关于javascript - jQuery 或 JavaScript 使用元素中的嵌入字符串作为函数引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14467947/

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