为了做到这一点,你需要将处理程序附加到页面上的特定锚。对于像这样的操作,使用标准框架如jQuery要容易得多。例如,如果我有以下HTML
HTML:
<a id="theLink">Click Me</a>
我可以使用下面的jQuery来装一个事件的具体环节。
// Use ready to ensure document is loaded before running javascript
$(document).ready(function() {
// The '#theLink' portion is a selector which matches a DOM element
// with the id 'theLink' and .click registers a call back for the
// element being clicked on
$('#theLink').click(function (event) {
// This stops the link from actually being followed which is the
// default action
event.preventDefault();
var answer confirm("Please click OK to continue");
if (!answer) {
window.location="http://www.continue.com"
}
});
});
感谢,如果我点击取消尽管它仍然打开链接/ – user1022585 2012-01-11 03:13:44
@ user1022585您需要返回false来停止链接导航。看到我的回答下面 - 确认'方法返回一个'布尔',你可以退出你的功能... – xandercoded 2012-01-11 03:18:38
对不起,我复制你的代码,并且你的代码是错误的。如果用户说“好”,答案将设置为“真”,因此我们不需要“!”,我改变了它。 – ocanal 2012-01-11 03:21:57