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;
答案 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));