我有称为网页浏览量的会话变量。加载页面后,我将执行一次检查以查看其是否等于2。如果确实要执行代码以打开窗口,则进行检查。现在,我可以单击链接打开该窗口,它可以正常工作。加载页面后如何自动打开它。
我页面中的Jquery和Session Checkc代码
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.pack.js"></script>
<% if (Session["PagesViewed"].ToString() == "2")
{ %>
<script type="text/javascript">
$(document).ready(function () {
//select all the a tag with name equal to modal
$('a[name=modal]').click(function (e) {
//Cancel the link behavior
e.preventDefault();
//Get the A tag
var id = $(this).attr('href');
//Get the screen height and width
var maskHeight = $(document).height();
var maskWidth = $(window).width();
//Set heigth and width to mask to fill up the whole screen
$('#mask').css({ 'width': maskWidth, 'height': maskHeight });
//transition effect
$('#mask').fadeIn(1000);
$('#mask').fadeTo("slow", 0.8);
//Get the window height and width
var winH = $(window).height();
var winW = $(window).width();
//Set the popup window to center
$(id).css('top', winH / 2 - $(id).height() / 2);
$(id).css('left', winW / 2 - $(id).width() / 2);
//transition effect
$(id).fadeIn(2000);
});
//if close button is clicked
$('.window .close').click(function (e) {
//Cancel the link behavior
e.preventDefault();
$('#mask').hide();
$('.window').hide();
});
//if mask is clicked
$('#mask').click(function () {
$(this).hide();
$('.window').hide();
});
});
</script>
<% } %>
该链接用于打开页面。但是我想在页面加载时自动执行此操作。
<a href="#dialog" name="modal">Simple Window Modal</a>
最佳答案
更改
$('a[name=modal]').click(function (e) {
//Cancel the link behavior
e.preventDefault();
//Get the A tag
var id = $(this).attr('href');
至
//Get the A tag
var id = $('a[name=modal]').attr('href');
并删除});;后
$(id).fadeIn(2000);
});
关于c# - 在页面加载时调用Jquery函数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8703465/