2021-10-10 07:40

JS中的Title: window.location.href跳转集无效

导读JS中的window location href跳转集无效怎么办?问题如下:原因是标签a的href跳转会在window location href设置的跳转之前执行,如果是表单

JS中的window.location.href跳转集无效怎么办?

问题如下:

window.location.href

原因是标签a的href跳转会在window.location.href设置的跳转之前执行,如果是表单表单,会先执行表单提交。提交后,不再在当前页面。因此,window.location.href无效。

解决方案1。

在js函数中加入“window.event.returnValue=false”,如图,这个属性在提交表单时放入onclick事件中,表单在这个click事件中不会被提交,如果放入hyperlink,则在这个click事件中不会执行hyperlink href属性。Window.location.href在更改为以下代码后成功跳转。

window.location.href

解决方案2。

单击事件中的onclick='checkUser()',变为onclick=' return CheckUser();'并在checkUser中返回false在这种情况下,将不会执行标记a的href。这样,window.location.href可以顺利跳转。代码:如下。

window.location.href

解决方案3。

如果是窗体提交,也可以将summit改为button调用js提交,这样window.location.href也会在js提交summit之前成功跳转。如下所示:

window.location.href

后记:这就是JS中window.location.href集不能跳转的原因,以及JS中window.location.href集不能跳转时的三种解决方案。