为了配合都非常-正确答案一起,发生了什么事是你内联,你已经写了onclick="return runFunction();"
如果你看看那个,什么它真正做的是要这样的功能:
var link = document.getElementById("myLink");
link.onclick = function() { runFunction(); };
看到问题了吗?我的runFunction
被调用时没有传入任何事件对象。 ......这意味着var thisLink = (evt) ?
将返回false,这意味着它将尝试在oldIE模式下运行。
通过写onclick="runFunction"
,这等于说:
link.onclick = runFunction;
这意味着当onclick事件发生,runFunction将被调用,并在符合W3C的浏览器,它会发送一个事件对象。
这就是解决方案的原因。
避免大量混淆的最佳方法是处理来自JavaScript内部的JavaScript以及处理HTML内部的HTML,以便您不必担心字符串如何转换为代码。
现在,让这一切工作,防止重定向,要做到这一点:
为W3C的浏览器(即传递事件参数的那些):
function runFunction (evt) {
// stops the default-action from happening
// means you need to find another way to fire it, if you want to later
evt.preventDefault();
// stops higher-up elements from hearing about the event
// like if you stop a submit button from "clicking", that doesn't stop the form
// from submitting
evt.stopPropagation();
//the oldIE versions of both of these are
event.cancelBubble = true;
event.returnValue = false;
}
这意味着没有事件被传递给函数。感谢名单。 – Vicky 2012-07-19 19:34:42