停止在 javascript 中重定向
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15811079/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Stop redirecting in javascript
提问by kosnkov
Hi am developing a webpart which is included in sharepoint app and I need to stop redirection in some cases. But it doesn't work, i am trying to use
您好,我正在开发一个包含在 sharepoint 应用程序中的 webpart,在某些情况下我需要停止重定向。但它不起作用,我正在尝试使用
$('a').click(function(event){
event.preventDefault();
event.stopImmediatePropagation();
return false;
});
it executes and anyway redirection is done. How can I break it?
它执行,无论如何重定向完成。我怎样才能打破它?
Purpose of it is: when user change sth on page I need to ask if he wants to proceed or not and then stop any redirection from othe links he might clicked.
它的目的是:当用户更改页面上的某个内容时,我需要询问他是否要继续,然后停止从他可能单击的其他链接进行的任何重定向。
Edit: I just checked and with regular a with links inside it works but the problem is with link like this:
编辑:我刚刚检查并使用常规 a 链接,它可以正常工作,但问题出在这样的链接上:
<a title="Delivery And Technology" class="ms-cui-ctl-large" id="someId" role="button" onclick="return false;" href="javascript:;" unselectable="on" mscui:controltype="" jQuery182001210093900916337="93">
which has inside this html
这个 html 里面有
<SPAN class=ms-cui-ctl-largeIconContainer unselectable="on"><SPAN class=" ms-cui-img-32by32 ms-cui-img-cont-float ms-cui-imageDisabled" unselectable="on"><IMG style="TOP: -96px; LEFT: -160px" alt="Delivery And Technology" src="/_layouts/1033/images/ps32x32.png" unselectable="on"></SPAN></SPAN><SPAN class=ms-cui-ctl-largelabel unselectable="on">Delivery And<BR>Technology</SPAN>
so seems that when I click on this java script recognize it and redirects me so what i want to achive is to detect it and stop before it will redirect me to other page.
所以似乎当我点击这个java脚本时会识别它并重定向我所以我想要实现的是检测它并在它将我重定向到其他页面之前停止。
回答by karaxuna
This will prevent dynamically added anchors from navigating too:
这也将阻止动态添加的锚点导航:
$(document).on('click', 'a', function(e){
e.preventDefault();
e.stopImmediatePropagation();
return false;
});
steveukx suggested shorthand:
steveukx 建议的速记:
$(document).on('click', 'a', false);
回答by Christofer Eliasson
My best guess is that you run this code before the DOM is ready. Try wrap it in a DOM-ready callback:
我最好的猜测是您在 DOM 准备好之前运行此代码。尝试将其包装在DOM 就绪回调中:
$(function () {
$('a').click(function(event){
return false;
});
});
Note that returning false is equivalent to .preventDefault()
and .stopImmediatePropagation()
together, so just returning false will be sufficient. In your case it might be more appropriate to just use .preventDefault()
and nothing else though.
请注意,返回 false 等价于.preventDefault()
and.stopImmediatePropagation()
在一起,因此仅返回 false 就足够了。在您的情况下,仅使用可能更合适,.preventDefault()
而没有其他任何用途。
回答by steveukx
If the element has a had a handler attached before your code executes, it isn't possible to reliably remove or prevent the handler from running.
如果元素在您的代码执行之前附加了一个处理程序,则不可能可靠地删除或阻止该处理程序运行。
Assuming you have jQuery
available, and you are running this function after the element has been added to the DOM and had its handlers attached you can replace it with an identical element:
假设您jQuery
可用,并且在将元素添加到 DOM 并附加其处理程序之后运行此函数,您可以将其替换为相同的元素:
jQuery('a').replaceWith(function(index, innerHTML) {
return jQuery(this.cloneNode(false)).html(innerHTML);
});