Js日历月和年问题 [英] Js calendar month and year issue
本文介绍了Js日历月和年问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我遇到的问题是,当我点击日期时,月份没有出现在日历上,它会回到默认月份。示例:我点击了9月21日,当操作完成后,日历会刷新回到7月(默认月份)。
我尝试了什么:
CodePen jsCalendar示例
解决方案
当您在某一天点击< a>
时,执行dayClicked
更新日历的功能。但是你没有阻止默认操作,即导航到重新加载页面的href
。
您可以删除href
,这会阻止导航。但这也会影响链接的风格:
(#days)。append(< td class ='day_active'>< a onclick ='dayClicked(+月+,+年+);'>+ i +< / a>< / td>);
或者,您可以在调用dayClicked
return false; >功能:
(#days)。append(< td class ='day_active'>< a href =''onclick ='dayClicked(+月+,+年+);返回false;'>+ i +< / a>< / td>);
或者,您可以使用jQuery委托事件:
功能天数(月,年){
...
The problem i am having is that when i click a date the month doesn't appear on calendar it goes back to the default month. Example: i clicked on 21 of September and when the action is done the calendar refreshes back to July(default month).
What I have tried:
CodePen jsCalendar Example
解决方案
When you click on the<a>
for a particular day, you execute thedayClicked
function to update the calendar. But you don't prevent the default action, which is to navigate to thehref
, which reloads the page.
You could remove thehref
, which would prevent the navigation. But this would also affect the style of the link:
("#days").append("<td class='day_active'><a onclick='dayClicked(" + month + "," + year + ");'>" + i + "</a></td>");
Or, you could addreturn false;
after calling thedayClicked
function:
("#days").append("<td class='day_active'><a href='' onclick='dayClicked(" + month + "," + year + ");return false;'>" + i + "</a></td>");
Or, you could use jQuery delegated events:
function days(month, year) { ...
这篇关于Js日历月和年问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文