假设我们将.click()处理程序附加到回调中的anchor(<a>)标签上$(document).ready。该处理程序将取消默认操作(在之后href)并显示警报。
我想知道的是,回调将在何时准确执行,用户是否可以单击锚点(该文档已在浏览器中显示),但该事件尚未附加。
这是包含锚点的不同HTML页面,但是脚本的包含顺序不同。它们之间有什么区别(如果有)?不同的浏览器会表现不同吗?
第1页:
<html>
<head>
<title>Test 1</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
$('a').click(function() {
alert('overriding the default action');
return false;
});
});
</script>
</head>
<body>
<a href="http://www.google.com">Test</a>
</body>
</html>
第2页:
<html>
<head>
<title>Test 1</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"></script>
</head>
<body>
<a href="http://www.google.com">Test</a>
<script type="text/javascript">
$(function() {
$('a').click(function() {
alert('overriding the default action');
return false;
});
});
</script>
</body>
</html>
Page3:
<html>
<head>
<title>Test 1</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<a href="http://www.google.com">Test</a>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
$('a').click(function() {
alert('overriding the default action');
return false;
});
});
</script>
</body>
</html>
因此,是否有可能href通过单击锚点将用户重定向到,而从没有看到警报(当然,忽略javascript禁用的情况)?我提供的任何示例都可能发生这种情况吗?
我需要做的就是确保click事件处理程序已附加到锚点上,然后用户才有可能单击该锚点。我知道我可以提供一个空的href,然后在javascript中逐步增强它,但这是一个更普遍的问题。
如果HTML是由Web服务器快速生成但jquery库需要花费时间从远程服务器获取的,将会发生什么情况?这会影响我的情况吗?与加载DOM相比,包含脚本的顺序是什么?它们可以并行完成吗?
onclick或附加click处理程序(不使用$(document).ready)?