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

时间:2015-08-06 07:41:24

标签: javascript jquery function switch-statement location-href

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;

1 个答案:

答案 0 :(得分:0)

Because you are invoking the function by adding () at the end of onHref.

One solution is to use a anonymous function as the event handler then call onHref inside it

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

Another option is to use Function.bind() to create a function reference like

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