我正在尝试编写一个小函数,它从 html 中获取一个 div 元素,隐藏它,分配一个值和一个变量名,然后将其解析到函数中以检查分配的月份(值)是否等于当前月份。我认为它不起作用的原因是“this”被转换为对象对象并且它不存储解析到其中的变量的值。
var currentMonth = (new Date).getMonth() + 1;
var valentinesdiv = $('#valentinesmenu');
$(valentinesdiv).hide();
valentinesdiv = 5;
$.fn.datechecker1 = function () {
if ($(this) == currentMonth) {
this.show();
} else {
alert(this);
}
};
$(valentinesdiv).datechecker1();
它只是直接进入警报框并输出对象对象,因此当我解析它时它不保存变量的值。
最佳答案
我看到的几个问题是,您尝试在纯 JS this
对象上使用 jQuery show()
,并且您看到 警报框中的 Object 对象
,因为 alert(this)
实际上是传入对象,而不是对象内的某个值。
您最可能想要的东西就在这里:
var element = $(this), value = element.val();
if(value == currentMonth){
element.show();
} else {
alert(value);
}
关于javascript - this 在 jQuery 中被转换为对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30144422/