我在这里有一个测试页面:http://www.problemio.com/test.php
如果按"单击以测试注册",您将获得一个表单.如果在该表单上,单击"登录",它会识别您单击该表单,然后打开登录表单.
但问题是,在登录表单上,如果按"创建配置文件",它实际上会转到href标记的url而不是jQuery click事件.
我的问题是这样做的最佳做法是什么?我发现了一种叫做"防止默认行为"的东西,但不知道应该如何/何时使用它.
我猜测如果用户已禁用JS,他们应该仍然可以登录.我如何设置它以便用户可以首先以jQuery方式登录和创建帐户,如果他们禁用JS则采用默认方式?
谢谢!
您可以使用纯jQuery执行此操作
$("#createprofilelink").click(function(event) {
event.preventDefault();
{create profile logic}
});
Run Code Online (Sandbox Code Playgroud)
有关详细信息,请参阅 jQuery文档http://api.jquery.com/event.preventDefault/
编辑:我删除了这个因为@maxedison注释它停止jQuery事件被触发但我刚刚测试了这个并且jQuery事件触发但是链接没有转到地址.
<a id="thelink" href="http://www.google.com" onclick="return false;">the link</a>
<script>
$('#thelink').click(function(){alert('alert me');});
</script>
Run Code Online (Sandbox Code Playgroud)
至于JS被禁用的部分问题,链接真的应该指向一个真实的表单来填充,正如Taryn East正确说的那样,即使用户体验因不使用JavaScript而降低,用户也会获得相同的功能.
你甚至可以沿着noscript路线走下去
<noscript>
<div>Your user experience would be far improved if you
enable JavaScript but if you insist,
<a href="{real link}">Click Here to create your profile</a></div>
</noscript>
Run Code Online (Sandbox Code Playgroud)