[英]Page reloading after calling jquery function
I have a div which I am checking for a condition at $(document).ready(function ())
, then deciding whether to show it or not. 我有一个div,我正在
$(document).ready(function ())
处检查条件,然后决定是否显示它。
There is a button inside the div which fades out the entire div. div内有一个按钮,可淡出整个div。 The problem is after calling the
fadeout()
function, the $(document).ready(function ())
gets called again(ie the page gets reloaded) and it again satisfies the condition to show the div. 问题是调用了
fadeout()
函数后,再次调用$(document).ready(function ())
(即重新加载页面),并且它再次满足显示div的条件。
I want to prevent the page from reloading after the fadeout()
function gets called. 我想防止在调用
fadeout()
函数后重新加载页面。
HTML 的HTML
<div class="success"><%: ViewData["successResult"] %>
<br /><br />
<span id="closenotification" class="buttons" onclick="fadeoutbox()"><a href="" ><img src="<%= Url.Content("~/Content/Images/door_out.png") %>" alt="" />Close</a></span>
</div>
javascript javascript
function fadeoutbox() {
$(".success").fadeOut(5000, function () {
/*$(".success").css("display", "none");
$("#closenotification").css("display", "none");*/
});
}
$(function () {
var successres = '<%= ViewData["successResult"] %>';
//alert(successres != null);
if (successres != "") {
var leftpos = screen.width / 2 - 350;
var toppos = screen.height / 2 - 125;
$(".success").css("left", leftpos);
$(".success").css("top", toppos);
$(".success").css("display", "block");
$("#closenotification").css("display", "block");
}
if ($(".failure").html() != "") {
var leftpos = screen.width / 2 - 350;
var toppos = screen.height / 2 - 125;
$(".failure").css("left", leftpos);
$(".failure").css("top", toppos);
$(".failure").css("display", "block");
}
});
You've wrapped the image in a a
tag. 你在一个包裹像
a
标签。 You'll need to use preventDefault to prevent the page from reloading on the click event of the a
tag. 您需要使用preventDefault来防止页面在
a
标签的click事件上重新加载。 or remove the a
tag altogether. 或删除
a
共标记。
Try to move the onclick
event into the link element (nested into the span
) and change your function like so 尝试将
onclick
事件移动到link元素中(嵌套在span
),然后像这样更改函数
function fadeoutbox(evt) {
evt.preventDefault();
$(".success").fadeOut(5000, function () {
...
});
}
Remove the onclick="fadeoutbox()"
part and add this handler: 删除
onclick="fadeoutbox()"
部分,然后添加以下处理程序:
$('#closenotification').click(function(e) {
e.preventDefault();
fadeoutbox();
});
Try this : 尝试这个 :
$(function () { $(函数(){
var successres = '<%= ViewData["successResult"] %>';
//alert(successres != null);
if (successres != "") {
var leftpos = screen.width / 2 - 350;
var toppos = screen.height / 2 - 125;
$(".success").css("left", leftpos);
$(".success").css("top", toppos);
$(".success").css("display", "block");
$("#closenotification").css("display", "block");
return;
}
if ($(".failure").html() != "") {
var leftpos = screen.width / 2 - 350;
var toppos = screen.height / 2 - 125;
$(".failure").css("left", leftpos);
$(".failure").css("top", toppos);
$(".failure").css("display", "block");
return;
}
}); });
you can do one of these : 您可以执行以下操作之一:
#
for href #
设为href fadeoutbox
fadeoutbox
的末尾返回false e.preventDefault()
like other guys said e.preventDefault()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.