我可以附加一个函数作为按钮的单击事件,使浏览器返回上一页而不使用history.go()函数和window.location.href吗?
<input type="submit" name="cancel" value="cancel" onclick="history.go(-1)"/>
此解决方案的好处是可以在悬停时显示链接到页面的URL,正如大多数浏览器默认情况下那样,而不是history.go(-1)或类似的:
1 <script>
2 document.write('<a href="' + document.referrer + '">Go Back</a>');
3 </script>'
试试这个
<input type="submit" name="cancel" value="cancel" onclick="goPrev()"/>
<script>
function goPrev()
{
window.history.back();
}
</script>
同意Simon,最好让用户知道链接的目标是什么。在此解决方案中,您将打印一个链接:
<script>
document.write('<a href="' + document.referrer + '">Go Back</a>');
</script>
但如果您真的想使用输入按钮,那么您可以使用Nikhila命题:
<input type="submit" name="cancel" value="cancel" onclick="goPrev()"/>
<script>
function goPrev()
{
window.history.back();
}
</script>
注意这只是JS代码,链接或按钮将出现在将添加脚本块的位置。如果需要,可以添加使用JQuery来指定添加新内容的位置,或者使用appendChild(请参阅https://www.w3schools.com/jsref/met_node_appendchild.asp中的代码)
祝你今天愉快