我的代码中的“Var”不起作用。
它总是显示'hasclass = 0 and body has class'
,但应该hasclass = yes and body has class
setInterval(function() {
if($('.webMessengerMessageGroup').length>0) {
if(hasclass === 'dooainn') {
console.log('hasclass = yes and body has class');
} else {
var hasclass = 'dooainn';
console.log('hasclass = 0 and body has class');
$('._38 p').click(function () {
var message_text = $(this).text();
console.log(message_text);
});
}
} else {
console.log('body has not class');
}
},2000);
最佳答案
var hasclass 未在 if 范围内定义。如果将 var 移到函数之外,如下所示,它应该可以工作。
var hasclass = '';
setInterval(function() {
if($('.webMessengerMessageGroup').length>0) {
if(hasclass === 'dooainn') {
console.log('hasclass = yes and body has class');
} else {
hasclass = 'dooainn';
console.log('hasclass = 0 and body has class');
$('._38 p').click(function () {
var message_text = $(this).text();
console.log(message_text);
});
}
} else {
console.log('body has not class');
}
},2000);
关于javascript - 我的代码中的 Var 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31007891/