我一直在使用一个非常简单的模式,使用弹出的blockUI并询问用户是否属于某个年龄。我总是首先尝试在它自己的页面中开发一段代码以避免冲突,这就是我在这里所做的。我有一个简单的html / javascript页面,它没有正常运行。
每当页面加载时,它就会很好。但是,当尝试取消阻止时(即使不使用按钮)它也没有做任何事情。它只是坐在那里加载图标。
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript" src="jquery.blockUI.js"></script>
<script type="text/javascript">
$(document).ready(function() {
// A bit of a work-around. BlockUI tries to center based on location of
// the div. By attaching block to the html tag immediately, we avoid
// this issue completely.
$('html').block({
message: $('#message'),
centerX: true,
centerY: true,
css: {
width: '600px',
height: '300px',
border: '3px solid #FF9900',
backgroundColor: '#000',
color: '#fff',
padding: '25px'
}
});
$('#over').click(function() {
alert('clicked!');
$.unblockUI();
return false;
});
$('#under').click(function() {
$.unblockUI();
return false;
});
});
</script>
</head>
<body>
<div id="message" style="display:none;">
<img src="logo.png" border="0" />
<br />
<h1>Welcome!</h1>
You may not view this page unless you are 21 or over.<br />
<button id="over">I am 21 or over</button> <button id="under">Under 21</button>
</div>
It's dusty under here! Let me be seen!
</body>
</html>
在Chrome的控制台中没有出现任何错误,我无法找到不应该关闭的原因。
答案 0 :(得分:4)
当您致电$.unblockUI()
时,请尝试在元素上调用它,例如。 $('html').unblock();
:
<div class="body">
<div id="message" style="display:none;">
<img src="logo.png" border="0" />
<br />
<h1>Welcome!</h1>
You may not view this page unless you are 21 or over.
<br />
<button id="over">I am 21 or over</button>
<button id="under">Under 21</button>
</div>
It's dusty under here! Let me be seen!
</div>
JS将是:
$('.body').block({
message: $('#message'),
centerX: true,
centerY: true,
css: {
width: '600px',
height: '300px',
border: '3px solid #FF9900',
backgroundColor: '#000',
color: '#fff',
padding: '25px'
}
});
$('#over').click(function () {
alert('clicked!');
$('.body').unblock();
return false;
});
$('#under').click(function () {
$('.body').unblock();
return false;
});
参见工作示例:http://jsfiddle.net/amyamy86/Taw83/
在此处详细了解元素阻止:http://www.malsup.com/jquery/block/#element