如何在javascript函数中使用html标记建立列表
我已经编写了用javascript显示日期(月、日、日、年)的代码,并将其放在ul中 我的问题是如何在javascript函数中建立一个带有html标记的li(这样我就可以在函数中放置分隔符),因为现在如果我想执行以下操作如何在javascript函数中使用html标记建立列表,javascript,html,tags,Javascript,Html,Tags,我已经编写了用javascript显示日期(月、日、日、年)的代码,并将其放在ul中 我的问题是如何在javascript函数中建立一个带有html标记的li(这样我就可以在函数中放置分隔符),因为现在如果我想执行以下操作 var output = "<li>" + month_full[monthFull] + "<br/>" + weekDay[day] + "</li>"; return output; var output=“”+月整[月整]+”“
var output = "<li>" + month_full[monthFull] + "<br/>" + weekDay[day] + "</li>";
return output;
var output=“”+月整[月整]+”
“+工作日[日]+” ”;
返回输出;
它将准确地输出其显示方式->
你可以水平滚动我的月份。现在,我想这样构建它:
要查看我的JSFIDLE以查看我现在拥有的内容:
我的javascript代码如下所示:
<script>
// Get today's current date.
var now = new Date();
console.log(now);
// Calculates the number of the week
var weekNumber = ((now.getDate()<10) ? "0" : "")+ now.getDate();
console.log("The current week number is: " + weekNumber);
// Array list of months.
var month_full = new Array(12);
month_full[0] = "January";
month_full[1] = "February";
month_full[2] = "March";
month_full[3] = "April";
month_full[4] = "May";
month_full[5] = "June";
month_full[6] = "July";
month_full[7] = "August";
month_full[8] = "September";
month_full[9] = "October";
month_full[10] = "November";
month_full[11] = "December";
//console.log(month[3]);
var weekDay = new Array(7);
weekDay[0]= "Su";
weekDay[1] = "Mo";
weekDay[2] = "Tu";
weekDay[3] = "We";
weekDay[4] = "Th";
weekDay[5] = "Fr";
weekDay[6] = "Sa";
function weekNr_Month(date){
var month = date.getUTCMonth();
return month[month];
}
function formatDate(date) {
var month = date.getUTCMonth() +1;
var dayNumber = date.getUTCDate();
var year = date.getUTCFullYear();
var day = date.getUTCDay();
var monthFull = date.getUTCMonth();
return month_full[monthFull] + " " + weekDay[day] + ": " + dayNumber + "-" + month + "-" + year + "; ";
}
console.log(formatDate(new Date()));
var today
function addListItem(){
var createListItem = document.createElement("li");
var outputListItem = document.createTextNode(today);
createListItem.appendChild(outputListItem);
var createUl = document.getElementsByTagName("ul");
createUl[0].appendChild(createListItem);
}
// loop starting from may up untill for months from the set date
for (var i = 1; i < 122; i++){
today = formatDate(new Date(2015, 05, i));
//document.write(today + "<br />");
addListItem();
}
document.getElementById('timeline').
getElementsByTagName('li')[(new Date()).getDate() + 1].className += ' currentDate';
</script>
//获取今天的当前日期。
var now=新日期();
console.log(现在);
//计算周数
var weekNumber=((now.getDate()这是因为您正在创建一个文本节点而不是html内容。
试试这个:
var createListItem = document.createElement("li");
createListItem.innerHTML = today;
看看如何在
标记后添加列表。请注意,您可能不希望
中已经存在的ul
,这可能会打乱您的样式:-)