我試圖做一個jQuery UI的日曆使Ajax調用被點擊時的日期上, 但我前幾天遇到了一個問題。 我發現的代碼,理應做到這一點的一個片段,但我發現它使用jQuery的自定義選擇。該代碼給了我一個錯誤,所以我開始挖掘自定義選擇器,以瞭解更多關於它們。到目前爲止,我還沒有找到爲什麼我會得到這種奇怪的行爲。jQuery的定製選擇,「不確定」
這裏是一個圖片希望清楚的事情了,我將更多地解釋它
我在控制檯
$('.ui-datepicker-calendar td a:test(3)')
已經輸入後,你看,我meta2和stack2中未定義還有一件奇怪的事情,爲什麼index2返回一個#document,它應該包含元素數組的索引。
此外,該元件(EL2)甚至不是正確的元素。 看看,我叫
$('.ui-datepicker-calendar td a:test(3)')
這應該選擇所有從日曆的日期,並在第一循環中,執行console.log應打印出此
<td class=" ui-datepicker-week-end " data-handler="selectDay" data-event="click" data-month="8" data-year="2012"><a class="ui-state-default" href="#">1</a></td>
而是我得到的第一個「一」的標籤在整個文檔中,在這種情況下,它的一個前一個月(所看到的圖片)。
如果任何人都可以在這種情況下,灑下一盞小燈,請做。 哦,還有一件事我forgout約
meta2,它應該包含這個
[
':test(argument)', // full selector
'test', // only selector
'', // quotes used
'argument' // parameters
]
在我的情況下,其不確定
,並再次...
我將分享我的javascript代碼我希望它幫助
<script>
$(function()
{
$.expr[":"].test = function(el2,index2,meta2,stack2)
{
debugger;
console.log(el2);
console.log(index2);
console.log(meta2);
console.log(stack2);
}
})
$(function()
{
function getJsonDate(year, month)
{
$.getJSON('dates.php?year='+year+'&month='+month, function(data)
{
var i = 0;
for (i = 0; i < data.data.length; i++)
{
debugger;
var myDay = data.data[i]['d'];
$('.ui-datepicker-calendar td a:exactly('+data.data[i]['d']+')')
.css({color: '#f00'})
.attr('href',data.data[i]['link'])
.parent().attr('onclick','');
}
});
}
$.expr[":"].exactly = function(el, index, meta, stack)
{
debugger;
console.log(el);
console.log(index);
console.log(meta);
console.log(stack);
var s = meta[3];
if (!s) return false;
return eval("/^" + s + "$/i").test($(el).text());
};
$('#datepicker').datepicker(
{
inline: true,
onSelect: function(dateText, inst)
{
Date.prototype.toString = function() {return isNaN (this) ? 'NaN' : [this.getDate(), this.getMonth(), this.getFullYear()].join('/')}
d = new Date(dateText);
getJsonDate(d.getFullYear(), d.getMonth()+1);
},
onChangeMonthYear: function(year, month, inst)
{
//alert(year);
//alert(month);
getJsonDate(year, month);
}
});
});
</script>
我跑到今天早上花了2h相同的問題,對我來說元仍然是未定義的。以前工作完美,請保持更新,如果你會找到一些東西。謝謝。 –
你好@JakubKuchar,我沒有設法使它工作,所以我做了一個解決方法,並選擇了我需要的其他方式的元素。但我仍然很想知道答案。 – Jordashiro
在jQuery 1.8中更新了Sizzle,導致了對自定義選擇器創建的重要語法更改。請查看Sizzle文檔以獲取更多信息。 https://github.com/jquery/sizzle/wiki/Sizzle-Documentation它看起來像是如果你升級到jQuery 1.8.1,舊的選擇器將再次工作。 –