调用的接口为:中国天文台农历数据接口
更多功能自个研究
jQuery代码:
$(document).ready(function() {
var resultElement = $("#result");
function getDayInfo(date) {
var apiUrl = "https://api.asilu.com/lunar";
var requestData = {
data: date,
callback: "processResponse" // 回调函数的名称
};
// 创建全局的回调函数,用于处理响应
window.processResponse = function(response) {
var weekday = new Date(date).getDay();
var weekdayStr = "星期" + numToChinese(weekday);
var lunarDate = response.lunar_year_chinese + "年" + response.lunar_month_chinese + response.lunar_day_chinese;
var solarTerm = response.term;
var resultStr = date + " " + weekdayStr + " 农历" + lunarDate;
resultElement.html(resultStr);
};
// 动态创建一个 <script> 标签来发送请求
var script = document.createElement("script");
script.src = apiUrl + "?data=" + encodeURIComponent(date) + "&callback=processResponse";
document.head.appendChild(script);
}
function numToChinese(num) {
var chineseNums = ["日", "一", "二", "三", "四", "五", "六"];
return chineseNums[num];
}
var currentDate = new Date();
var formattedDate = currentDate.getFullYear() + "-" + (currentDate.getMonth() + 1) + "-" + currentDate.getDate();
getDayInfo(formattedDate);
});
html代码:
<div id="result"></div>
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END
暂无评论内容