Javascript 相对路径的document.location.href

Javascript 相对路径的document.location.href,javascript,Javascript,我想在单击按钮时重新定位到相对url: <button onclick="document.location.href='/Recruiters.aspx'"></button> 但这不起作用 有什么建议吗?您的onclick需要是一个处理程序。你可以这样做: <script> function redirect() { window.location.href='/Recruiters.aspx'; } <

我想在单击按钮时重新定位到相对url:

<button onclick="document.location.href='/Recruiters.aspx'"></button>

但这不起作用


有什么建议吗?

您的onclick需要是一个处理程序。你可以这样做:

<script>
    function redirect()
    {
        window.location.href='/Recruiters.aspx';
    }
</script>

<button onclick="redirect()"></button>
<button onclick="javascript: window.location.href='/Recruiters.aspx';"></button>

函数重定向()
{
window.location.href='/Recruiters.aspx';
}
如果您不想使用处理程序,您仍然可以这样内联使用:

<script>
    function redirect()
    {
        window.location.href='/Recruiters.aspx';
    }
</script>

<button onclick="redirect()"></button>
<button onclick="javascript: window.location.href='/Recruiters.aspx';"></button>

您的onclick需要是一个处理程序。你可以这样做:

<script>
    function redirect()
    {
        window.location.href='/Recruiters.aspx';
    }
</script>

<button onclick="redirect()"></button>
<button onclick="javascript: window.location.href='/Recruiters.aspx';"></button>

函数重定向()
{
window.location.href='/Recruiters.aspx';
}
如果您不想使用处理程序,您仍然可以这样内联使用:

<script>
    function redirect()
    {
        window.location.href='/Recruiters.aspx';
    }
</script>

<button onclick="redirect()"></button>
<button onclick="javascript: window.location.href='/Recruiters.aspx';"></button>

按钮的默认
类型为
“提交”
,因此在表单中单击按钮将其提交。虽然它应该首先重定向,但值得一试:

<button type="button" onclick="document.location.href='/Recruiters.aspx'"></button>

按钮的默认
类型为
“提交”
,因此在表单中单击按钮将其提交。虽然它应该首先重定向,但值得一试:

<button type="button" onclick="document.location.href='/Recruiters.aspx'"></button>


尝试使用
window.location.href
。按钮是否在表单中?@Brugnar…不在work@DavidVanStaden
window.location.href
无法修复它,但它比
document.location.href
更受欢迎。请尝试使用
window.location.href
。按钮是否在表单中?@Brugnar…否work@DavidVanStaden
window.location.href
无法修复它,但它比
document.location.href更可取。是的,你是对的。将其默认为type='Button'成功了。谢谢。或者,您可以让onclick脚本返回false,这将在保持按钮类型的同时取消提交(如果您出于某种原因需要它仍然是提交)。是的,您是对的。将其默认为type='Button'成功了。谢谢。或者,您可以让onclick脚本返回false,这将在保持按钮类型的同时取消提交(如果出于某种原因需要它仍然是提交)。