我在 jsp 的遗留项目中有以下代码:
<tr>
<th colspan="2">Customer</th>
<td colspan="2">
<a href="myAction.do" target="view">CustomerLink</a></td>
</tr>
当我们点击客户链接时,我不希望对行为进行少量修改。我只想在点击操作之前在 Javascript 函数中进行一些处理。 我不知道如何在 Javascript 函数中模拟 href 链接行为?
我尝试过:-
<tr>
<th colspan="2">Customer</th>
<td colspan="2">
<a href="javascript:customerLinkClicked('myAction.do')" target="view">CustomerLink</a></td>
</tr>
Javascript 函数
function customerLinkClicked(path)
{
// simulate the href link behaviour, not sure how to do this
}
<a href="javascript:customerLinkClicked('myAction.do')" target="view">CustomerLink</a>
<script>
function customerLinkClicked(path) {
if (confirm('message'))
document.location.href = path;
}
</script>
您可以使用 window.confirm 获取确认对话框,该对话框返回 true 或 false,具体取决于选择。之后,默认行为可能会被 event.preventDefault 方法取消。
示例:http://jsfiddle.net/Klaster_1/nXtvF/
$("a").on("click", function(event) {
if(!window.confirm("Proceed?")){
event.preventDefault()
};
});
我在里面使用了jQuery,但它是完全可选的。
怎么样:
<a href="myAction.do" onclick="customerLinkClicked()" target="view">CustomerLink</a>
首先,将处理 onclick 事件处理程序,然后将位置更改为 myAction.do。
如果要防止更改位置,可以写
onclick="return customerLinkClicked();"
根据返回值(真、假),位置将发生更改。要不。
试试这个。只需复制和粘贴
<script type="text/javascript">
function fnc()
{
if(confirm("Do you really want to go?"))
{window.open("yoursitename.do");}
}
</script
<a href="javascript:fnc()">click</a>
你应该避免内联js,但对于你当前的情况。
function customerLinkClicked(event) {
if(!confirm("Are you sure ?")) {
if(event.preventDefault) {
event.preventDefault();
}
else {
event.returnValue = false;
}
}
}
.HTML
<a href="myAction.do" onclick="customerLinkClicked(event);" target="view">CustomerLink</a>