jquery.tap 运行 没有事件的函数

jquery.tap run the function without event

function onHref(href){
    window.location.href = href;
}

当我 运行 代码自动重定向到最后一个 obj[key].link

switch ( obj[key].type ) {
        case 'href': //se href attribuisce link diretto alla pagina
            $(id).tap(onHref(obj[key].link));
            break;

因为你是通过在onHref末尾添加()来调用函数的。

一种解决方案是使用匿名函数作为事件处理程序,然后在其中调用 onHref

$(id).tap(function () {
    onHref(obj[key].link)
});

另一种选择是使用 Function.bind() 创建函数引用,例如

$(id).tap(onHref.bind(undefined, obj[key].link));