任何人都可以告诉我为什么下面的代码似乎没有阻止链接做它的事情?我知道我可以使用onclick="return false",但它应该与preventDefault一起使用,对吧?我试过onclick="function(e){this.preventDefault()}"和onclick="this.preventDefault()",但没有爱.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Test</title>
</head>
<body>
<a href="http://www.google.com" onclick="function(e){e.preventDefault()}">Google Search</a>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
Der*_*會功夫 14
删除function(e){.当你放在function(e){}那里,这意味着创建一个功能,但不运行它.
演示:http://jsfiddle.net/DerekL/RnngR/
像这样做:
<a href="http://www.google.com" onclick="event.preventDefault();">Google Search</a>
Run Code Online (Sandbox Code Playgroud)