javascript函数导致在asp.net上的回发

时间:2018-09-27 12:15:41

标签: javascript asp.net webforms code-behind

下面的JavaScript导致了asp.net网络表单代码背后的有害回发:

mini_cal.on("click, focusin", ".a-date", function(){
if(!$(this).hasClass('blurred'))
showEvent($(this).data('event'));
});`
它来自日历控件,当您单击日期时会调用该函数。添加e.preventDefault()会停止回发,但不会调用showEvent()。在普通的html页面上,一切正常,但是我需要在aspx页面上这样做,因为我需要从服务器端数据库中提取事件。我希望有人可以提出解决方案,因为作者本人似乎无法提出解决方案。

好,这是aspx页面:

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="caltest.aspx.vb" Inherits="caltest" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <link rel="stylesheet" href="css/mini-event-calendar.css" />
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
	<script src="js/mini-event-calendar.js?v=7"></script>

	<script>
	    var db_events = [
	    	{
	    	    title: "Board members meeting.",
	    	    date: 1532381440036,
	    	    link: "events.com/ev2"
	    	},
	        {
	            title: "Delaware branch opening.",
	            date: 1532467961534,
	            link: "events.com/ev1"
	        },
	        {
	            title: "An important event.",
	            date: 1532554405128,
	            link: "events.com/ev1"
	        }
	    ];

	    $(document).ready(function () {
	        $("#calendar").MEC({
	            calendar_link: "example.com/myCalendar",
	            events: db_events
	        });

	        //if you don't have events, use
	        //  $("#calendar2").MEC();
	    });
	</script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <h3>With events...</h3>
	<div id="calendar" runat="server" style="width: 30%;margin-right:auto;margin-left:auto;"></div> <br>
    </div>
    </form>
</body>
</html>

mini-event-calendar.js

(function( $ ) {
	var calenderTpl = '<div id="calTitle"><button class="month-mover prev"><svg fill="#FFFFFF" height="30" viewBox="0 0 24 24" width="30" xmlns="http://www.w3.org/2000/svg"><path d="M15.41 7.41L14 6l-6 6 6 6 1.41-1.41L10.83 12z"/><path d="M0 0h24v24H0z" fill="none"/></svg></button><div id="monthYear"></div><button class="month-mover next"><svg fill="#FFFFFF" height="30" viewBox="0 0 24 24" width="30" xmlns="http://www.w3.org/2000/svg"><path d="M10 6L8.59 7.41 13.17 12l-4.58 4.59L10 18l6-6z"/><path d="M0 0h24v24H0z" fill="none"/></svg></button></div><div><div id="calThead"><div>S</div><div>M</div><div>T</div><div>W</div><div>T</div><div>F</div><div>S</div></div><div id="calTbody"></div></div><div id="calTFooter"><h3 id="eventTitle">No events today.</h3><a href="javascript:void(0);" id="calLink">ALL EVENTS</a></div>';
	var short_months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul","Aug", "Sep", "Oct", "Nov", "Dec"];
	var today = new Date();
	var cur_month = today.getMonth();
	var cur_year = today.getFullYear();

    $.fn.miniEventCalendar = $.fn.MEC = function(options) {
    	var settings = $.extend({
    		calendar_link : "",
    		events: []
        }, options );

        var mini_cal = this;

        mini_cal.addClass('mini-cal').html(calenderTpl);

        var tbody = mini_cal.find("#calTbody");
		var cal_title = mini_cal.find("#monthYear");
		var cal_footer = mini_cal.find("#calTFooter");
        var event_title = mini_cal.find("#eventTitle");
		var events_link = mini_cal.find("#calLink");

		cal_title.text("Feb 2018");
        event_title.text("No events today.");
		events_link.text("ALL EVENTS");
		events_link.attr("href", settings.calendar_link);

		if(!settings.calendar_link.length && !settings.events.length)
			cal_footer.css("display", "none");

		mini_cal.find(".month-mover").each(function(){
			var mover = $(this);
			mover.bind("click", function(){
				if(mover.hasClass("next"))
					viewNextMonth();
				else
					viewPrevMonth();
			});
		});

		mini_cal.on("click, focusin", ".a-date", function(){
		    if(!$(this).hasClass('blurred'))
		        showEvent($(this).data('event'));
		});

		function populateCalendar(month, year) {
			tbody.html("");
			cal_title.text(short_months[month] + " " + year);

			cur_month = month;
			cur_year = year;

			var ldate = new Date(year, month);
			var dt = new Date(ldate);

			if(ldate.getDate() === 1 && dt.getDay() != 1)
					tbody.append(last_prev_month_days(dt.getDay()));

			while (ldate.getMonth() === month) {
     			dt = new Date(ldate);

     			var isToday = areSameDate(ldate, new Date());
     			var event = null;
     			var event_index = settings.events.findIndex(function(ev) {
		     		return areSameDate(dt, new Date(ev.date));
		     	});

		        if(event_index != -1){
		        	event = settings.events[event_index];

		        	if(isToday)
		        		showEvent(event);
		        }

     			tbody.append(date_tpl(false, ldate.getDate(), isToday, event));

     			ldate.setDate(ldate.getDate() + 1);

     			var buffer_days = 43 - mini_cal.find(".a-date").length;

		        if(ldate.getMonth() != month)
		        	for (var i = 1; i < buffer_days; i++)
		     			tbody.append(date_tpl(true, i));
     		}
 		}

 		function last_prev_month_days(day){
 			if(cur_month > 0){
     			var month_idx = cur_month - 1;
     			var year_idx = cur_year;
     		}else{
     			if(cur_month < 11){
     				var month_idx = 0;
     				var year_idx = cur_year + 1;
     			}else{
     				var month_idx = 11;
     				var year_idx = cur_year - 1;
     			}
     		}
     		
     		var prev_month = getMonthDays(month_idx, year_idx);
     		var last_days = "";
        	for (var i = day; i > 0; i--)
     			last_days += date_tpl(true, prev_month[ prev_month.length - i]);

        	return last_days;
 		}

		function date_tpl(blurred, date, is_today, event){
			var tpl = "<div class='a-date blurred'><span>"+date+"</span></div>";

			if(!blurred){
		        var cls = is_today ? "current " : "";
		        cls += event && event !== null ? "event " : "";
		        
		        var tpl ="<button class='a-date "+cls+"' data-event='"+JSON.stringify(event)+"'><span>"+date+"</span></button>";
			}

			return tpl;
		}

		function showEvent(event){
			if(event && event !== null && event !== undefined){
				event_title.text(event.title);
				events_link.text("VIEW EVENT");
				events_link.attr("href", event.link);
			}else{
				event_title.text("No events on this day.");
				events_link.text("ALL EVENTS");
				events_link.attr("href", settings.calendar_link);
			}
		}

		function viewNextMonth(){
			var next_month = cur_month < 11 ? cur_month + 1 : 0;
			var next_year = cur_month < 11 ? cur_year : cur_year + 1;

			populateCalendar(next_month, next_year);
		}

		function viewPrevMonth(){
			var prev_month = cur_month > 0 ? cur_month - 1 : 11;
			var prev_year = cur_month > 0 ? cur_year : cur_year - 1;
			
			populateCalendar(prev_month, prev_year);
		}

		function areSameDate(d1, d2) {
			return d1.getFullYear() == d2.getFullYear()
		        && d1.getMonth() == d2.getMonth()
		        && d1.getDate() == d2.getDate();
		}

		function getMonthDays(month, year) {
		     var date = new Date(year, month, 1);
		     var days = [];
		     while (date.getMonth() === month) {
		        days.push(date.getDate());
		        date.setDate(date.getDate() + 1);
		     }
		     return days;
		}

		populateCalendar(cur_month, cur_year);

        return mini_cal;
    };
 
}( jQuery ));

mini-event-calendar.css

.mini-cal{
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;

    font-family: Verdana, sans-serif;
    padding-bottom: 1.2em;
    background: #22252e;
    color: #fff;
}

#calTitle{
    display: flex;
    justify-content: space-between;
    -ms-align-items: center;
    align-items: center;
    font-size: 1.12em;
    text-align: center;
    padding: 0.4em 1em;
    padding-top: 0.8em;
}

#calTitle button{
    outline: none;
    display: block;
    border: 0.1em solid #ddd;
    border: none;
    padding: 0;
    width: 40px;
    height: 40px;
    line-height: 60px;
    border-radius: 50%;
    background-color: rgba(0, 0, 0, 0.1);
}

#calTitle button svg{
    width: 30px;
    height: 30px;
}

#calTitle button:hover{
    background: rgba(255,255,255,0.1);
}

#calThead, #calTbody{
    display: flex;
    flex-wrap: wrap;
    padding: 0.1em;
}

#calThead{
    color: #fff;
    margin-top: 0.4em;
    align-items: center;
    text-align: center;
    font-size: 0.88em;
}

#calThead > div, #calTbody .a-date{
    box-sizing: border-box;
    flex: 1;
    min-width: calc(100% / 7);
    max-width: calc(100% / 7);
    width: calc(100% / 7);
    text-align: center;
    padding: 0;
}

#calThead > div{
    font-size: 1.1em;
    padding: 0.2em 0.2em;
}

#calTbody{
    color: #ddd;
}

#calTbody .a-date > span{
    display: block;
    font-size: 1em;
}

#calTbody .a-date{
    cursor: default;
    padding: 0;
    position: relative;
    background-color: transparent;
    color: inherit;
    padding: 1em;
    border: 0.1em solid transparent;
    outline: none;
    font-size: 0.9em;
}

#calTbody .a-date.blurred{
    opacity: 0.5;
    pointer-events: none;
}

#calTbody .a-date.event:before{
    content: '';
    position: absolute;
    top: 0.2em;
    right: 0;
    left: 0;
    margin: auto;
    background-color: #fffc23;
    width: 0.3em;
    height: 0.3em;
    border-radius: 50%;
}

#calTbody .a-date.current{
    border-color: #fffc23;
    outline: none;
    outline: 0;
}

#calTbody .a-date.current.event{
    background-color: #fffc23;
    color: #000;
}

#calTbody .a-date:focus,
#calTbody .a-date:active{
    background: #3f4042;
}

#calTFooter{
    display: flex;
    justify-content: space-between;
    -ms-align-items: center;
    align-items: center;
    font-size: 1.1em;
    padding: 0 1em;
    margin-top: 0.5em;
}

#calTFooter #calLink{
    font-size: 0.8em;
    display: inline-block;
    padding: 0.6em 0.8em;
    flex-shrink: 0;
    text-decoration: none;
    color: #fffc23;
}

#calTFooter #calLink:hover{
    background-color: #555;
}

#calTFooter #eventTitle{
    margin: 0;
    margin-right: 0.1em;
    font-weight: normal;
    font-size: 0.95em;
    white-space: nowrap;
    overflow: hidden;
    -ms-text-overflow: ellipsis;
    text-overflow: ellipsis;

}

0 个答案:

没有答案