[英]Trying to build jquery interstital div, but no action when clicking on link
I am trying to build the following action using jquery: when a user clicks on a link, the mainContent div is hidden and the exitSite message is shown. 我正在尝试使用jquery构建以下操作:当用户单击链接时,mainContent div被隐藏,并且显示exitSite消息。 The user then has two choices: to return to the main content or to proceed and leave the site. 然后,用户有两个选择:返回主要内容或继续并离开站点。
The current issue: when clicking a link that will leave the site, nothing happens. 当前问题:单击将离开站点的链接时,没有任何反应。
Here is the code I have so far: 这是我到目前为止的代码:
the JQuery: jQuery的:
$(function(){
if($('div#exitSite'))
{
var mydomain$ = location.hostname;
var links = $('a').each(function()
{
if((this.href.indexOf("mySiteDomain")==-1) && (this.href.indexOf(mydomain$)==-1) && (this.href.indexOf("javascr")==-1))
$(this).addClass('exit');
});
/*Off site links.*/
$('a.exit').live('click', function (e) {
e.preventDefault(); //stop the click from going through.
var page = $(this).attr("href") //get destination page
var pagetitle = $(this).attr("title")
$('#mainContent').hide(); //hide main content window
$('#mainContent').show(); //show main content window
$('#exitSiteClose').click(function() {
$('#exitSite').hide();
$('#mainContent').show();
return false
}); // end click #exitSiteClose
$('#exitSiteContinue').click(function() {
window.location=page
//if user selects the continue button, then pass in the destination page from link.
}); // end click #exitSiteContinue
});// end click a.exit
}
});
the html: HTML:
link leaving site: click to stackoverflow.com 离开网站的链接: 单击到stackoverflow.com
the exit site div: 退出网站div:
<div id="exitSite">
<h2>Hello!</h2>
<p><strong>You are leaving this website</strong></p>
<div class="buttonContainer">
<a href="#" id="exitSiteClose" class="btn-gray">Cancel</a>
<a href="#" id="exitSiteContinue" class="btn-blue">Continue</a>
</div><!-- /.buttonContainer -->
</div><!-- /#exitSite -->
You could simply use a JavaScript confirm()
. 您可以简单地使用JavaScript confirm()
。 It's not stylish, but it's the most universal way to do what you want. 它不时尚,但却是您想要做的最通用的方式。
$('a[href^="http://"]').on('click', function(e) { // apply to all external URLs
return window.confirm('Are you sure you want to leave this site?');
});
Broadly speaking, any JavaScript function that will return false;
概括地说,任何将return false;
JavaScript函数return false;
from a click handler will cancel that click, and one that will return true;
从点击处理程序中将取消该点击,然后return true;
allows it to continue. 允许它继续。
A less intrusive, and more common, approach is to add target="_blank"
to any external links. 一种较不那么侵入且更常见的方法是将target="_blank"
到任何外部链接。 This will open that link in a new tab/window, keeping your site open in a separate one. 这将在新的标签/窗口中打开该链接,使您的网站保持在单独的位置。
You have a basic typo in your code. 您的代码中有一个基本的错字。 You are not showing your #exitSite
div at all: 您根本没有显示#exitSite
div:
$('#mainContent').hide(); //hide main content window
$('#mainContent').show(); //show main content window
Needs to be: 需要是:
$('#mainContent').hide(); //hide main content window
$('#exitSite').show(); //show exit confirmation window
Demo: http://jsfiddle.net/jtbowden/fhFxU/1/ 演示: http : //jsfiddle.net/jtbowden/fhFxU/1/
To debug, I used console.log("debug")
inside the click handler and watched in FireBug to see if the click handler was being called. 为了进行调试,我在点击处理程序中使用了console.log("debug")
,并在FireBug中进行了观察,以查看是否调用了点击处理程序。 Then I mentally stepped through the function to see what should happen. 然后,我在脑海中逐步完成了该功能,以了解会发生什么。 That's when I noticed you never showed your exit confirmation. 从那时起,我注意到您从未显示过退出确认信。
As a side note, you are also missing a couple semi-colons after your variable definitions in the handler. 附带说明一下,在处理程序中定义变量后,您还会缺少几个分号。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.