我有一个名为页面视图的会话变量。当我的页面加载时,我有一个检查,看它是否等于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>
答案 0 :(得分:1)
变化
$('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);
});
答案 1 :(得分:1)
以下是您可以自动触发该锚点击事件的方法:
$("a[name='modal']").click();
答案 2 :(得分:0)
$(document).ready(function () {
var openModal = function (e) {
// Cancel the link behavior if click event
e && e.preventDefault();
// Get the A tag or default to modal link href
var id = $(this).attr('href') || $('a[name=modal]').attr('href');
// ...REST OF CODE FROM FUNCTION...
};
$('a[name=modal]').click(openModal);
openModal();
});
答案 3 :(得分:0)
将代码放入$(document).ready(function () {})
类似的东西:
<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 () {
//Get the A tag
var id = $('a[name=modal]').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>