Javascript 这将在jQuery中转换为对象
我试图编写一个小函数,从html中获取一个div元素,隐藏它,分配一个值和一个变量名,然后将其解析到函数中,检查分配的月份(值)是否等于当前月份。我认为它不起作用的原因是“this”正在被转换成一个对象,并且它没有存储我正在解析的变量的值Javascript 这将在jQuery中转换为对象,javascript,jquery,html,Javascript,Jquery,Html,我试图编写一个小函数,从html中获取一个div元素,隐藏它,分配一个值和一个变量名,然后将其解析到函数中,检查分配的月份(值)是否等于当前月份。我认为它不起作用的原因是“this”正在被转换成一个对象,并且它没有存储我正在解析的变量的值 var currentMonth = (new Date).getMonth() + 1; var valentinesdiv = $('#valentinesmenu'); $(valentinesdiv).hide(); valentinesdiv = 5
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();
它直接进入警报框并输出object object,这样在我解析它时它就不会保存变量的值。我看到的几个问题是,您试图在纯JS
this
对象上使用jQueryshow()
实际上是传入对象,而不是对象内部的某个值
您最可能想要的是:
var element = $(this), value = element.val();
if(value == currentMonth){
element.show();
} else {
alert(value);
}
我看到的几个问题是,您试图在纯JS
this
对象上使用jQueryshow()
,并且您在警报框中看到了object对象,因为alert(this)
实际上是传入对象,而不是对象中的某个值
您最可能想要的是:
var element = $(this), value = element.val();
if(value == currentMonth){
element.show();
} else {
alert(value);
}
我看到的几个问题是,您试图在纯JSthis
对象上使用jQueryshow()
,并且您在警报框中看到了object对象,因为alert(this)
实际上是传入对象,而不是对象中的某个值
您最可能想要的是:
var element = $(this), value = element.val();
if(value == currentMonth){
element.show();
} else {
alert(value);
}
我看到的几个问题是,您试图在纯JSthis
对象上使用jQueryshow()
,并且您在警报框中看到了object对象,因为alert(this)
实际上是传入对象,而不是对象中的某个值
您最可能想要的是:
var element = $(this), value = element.val();
if(value == currentMonth){
element.show();
} else {
alert(value);
}
下面的js
假设实现是在特定的元素处设置变量x
:5
:#valentinesmenu
;然后检查变量是否等于变量currentMonth
x
:5
设置为#valentinesmenu
的text
$.fn.datechecker1
检查是否调用了元素,并且该元素的文本
是否等于当前月
;
如果为true,则调用此
元素上的.show()
,如果为true,则将此.text()、currentMonth、x
记录到控制台
不确定这是否是预期结果或要求;下面的js
基于上述假设
尝试使用.text()
var currentMonth=(新日期).getMonth()+1;
变量x=5;/?
var valentinesdiv=$(“#valentinesmenu”);
//将'valentinesdiv'的文本设置为'currentMonth'`
valentinesdiv.text(x.hide();
$.fn.datechecker1=函数(){
//如果`this.text()==currentmount`do stuff
if(this.text()==currentMonth){
this.show();
}否则{
//否则,做其他事情
console.log(this.text(),currentMonth,x);
}
};
valentinesdiv.datechecker1()代码>
js
下面假设实现是在特定的元素处设置变量x
:5
:#valentinesmenu
;然后检查变量是否等于变量currentMonth
x
:5
设置为#valentinesmenu
的text
$.fn.datechecker1
检查是否调用了元素,并且该元素的文本
是否等于当前月
;
如果为true,则调用此
元素上的.show()
,如果为true,则将此.text()、currentMonth、x
记录到控制台
不确定这是否是预期结果或要求;下面的js
基于上述假设
尝试使用.text()
var currentMonth=(新日期).getMonth()+1;
变量x=5;/?
var valentinesdiv=$(“#valentinesmenu”);
//将'valentinesdiv'的文本设置为'currentMonth'`
valentinesdiv.text(x.hide();
$.fn.datechecker1=函数(){
//如果`this.text()==currentmount`do stuff
if(this.text()==currentMonth){
this.show();
}否则{
//否则,做其他事情
console.log(this.text(),currentMonth,x);
}
};
valentinesdiv.datechecker1()代码>
js
下面假设实现是在特定的元素处设置变量x
:5
:#valentinesmenu
;然后检查变量是否等于变量currentMonth
x
:5
设置为#valentinesmenu
的text
$.fn.datechecker1
检查是否调用了元素,并且该元素的文本
是否等于当前月
;
如果为true,则调用此
元素上的.show()
,如果为true,则将此.text()、currentMonth、x
记录到控制台
不确定这是否是预期结果或要求;下面的js
基于上述假设
尝试使用.text()
var currentMonth=(新日期).getMonth()+1;
变量x=5;/?
var valentinesdiv=$(“#valentinesmenu”);
//将'valentinesdiv'的文本设置为'currentMonth'`
valentinesdiv.text(x.hide();
$.fn.datechecker1=函数(){
//如果`this.text()==currentmount`do stuff
if(this.text()==currentMonth){
this.show();
}否则{
//否则,做其他事情
console.log(this.text(),currentMonth,x);
}
};
valentinesdiv.datechecker1()代码>
js
下面假设实现是在特定的元素处设置变量x
:5
:#valentinesmenu
;然后检查变量是否等于varia