jQuery日历-.attr('id')获得错误的值 [英] Jquery Calendar - .attr('id') get wrong value
问题描述
我想通过将日历创建"功能中的div id
放置到标题文本中,以在排定标题中设置日期.
I want to set date in Schedule header by putting div id
from Calendar Creation function to Header text.
我会像这样$('.dayNumberCellValue').attr('id');
一样得到id
,但是我遇到一个问题,当您点击日历中的某天时,我得到的所有天都是相同的日期,即"27"(这是我日历的startDate).在我看来,$('.dayNumberCellValue').attr('id');
中的日期是相同的,但是为什么,如果他必须在所有单元格中仅显示"27"日期,日历如何正常工作?
I get id
like this $('.dayNumberCellValue').attr('id');
, but I have a problem that when you tap on day in calendar, I get the same date to all days, "27" (this is the startDate of my calendar). It seems to me that in $('.dayNumberCellValue').attr('id');
goes the same date, but why, and how the calendar is working fine if he must show only the "27" date in all cells?
点击功能->>
$('.div-cell').tap(function() {
var i;
var myDate2;
for (i = 0; i < 42; i++) {
$('#' + i + 'dayCell').removeClass('tapped');
}
$(this).toggleClass(
'tapped');
if($(this).hasClass('tapped')){
myDate2 = $('.dayNumberCellValue').attr('id');
$('#MSchedule header h4').text("S - "+myDate2);}
});
日历创建->
function setCalendar() {
var cTime = new Date();
var myDate = months[cTime.getMonth()] + " " + cTime.getFullYear();
var myDate2 = cTime.getDate() + " " + months[cTime.getMonth()];
$('#MCalendar header h4').text(myDate);
$('#MSchedule header h4').text("Schedule - " + myDate2)
var monthView = new Array(42);
var firstDayOfMonth = new Date(cTime.getFullYear(), cTime.getMonth(), 1);
while (firstDayOfMonth.getDay() != 1) {
firstDayOfMonth.addDays(-1);
}
var i;
for (i = 0; i < monthView.length; i++) {
startDate = new Date(firstDayOfMonth);
monthView[i] = startDate.addDays(i);
}
for(var day=0;day<monthView.length;day++){
var needDay=monthView[day].getDate();
var cell = $('#'+day+'dayCell');
var content = '<div class="dayNumberCellValue" id=' +needDay+ '>'
+ monthView[day].getDate()
+ '</div>';
cell.append(content);}
推荐答案
此行:
myDate2 = $('.dayNumberCellValue').attr('id');
将始终获取 first 匹配元素(文档中第一个具有"dayNumberCellValue"类的元素,从顶部开始)的id
值.
will always get the id
value of the first matching element (the first element with the class "dayNumberCellValue" in the document, starting at the top).
您还没有显示标记,但是如果您想要的元素在tapped元素内,那么在您的tap
处理程序内,您可以像这样找到它:
You haven't shown your markup, but if the element you want is inside the tapped element, then within your tap
handler you can find it like this:
myDate2 = $(this).find('.dayNumberCellValue').attr('id');
在this
元素内的 中查找第一个"dayNumberCellValue"元素,点击该元素将成为您正在观看tap
事件的元素.
That looks for the first "dayNumberCellValue" element within the this
element, which on tap will be the element you're watching the tap
event on.
离题:您说过总是得到"27",这表明您将"27"用作元素的id
.尽管最近已被允许(通过HTML5),但是在HTML 4和更早版本中无效,并保持在CSS中无效.我建议不要使用以数字开头的ID.实际上,我认为我可能会使用 data-*
属性来存储日期,而不是id
.
Off-topic: You've said you're always getting "27", which suggests you're using "27" as the id
of an element. While that's recently been allowed (by HTML5), it was invalid in HTML 4 and earlier, and remains invalid in CSS. I would recommend not using an ID starting with a digit. In fact, I think I'd probably use a data-*
attribute to store the date rather than id
.
这篇关于jQuery日历-.attr('id')获得错误的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!