jquery中的if/else
这里的代码很简单。我正在检查带有#setactionsuccess的节是否存在,如果不存在,我要创建它并将其预先添加到容器中。第一部分工作顺利,但它从来没有去'其他'(和条件得到满足-我三重检查)。非常感谢您的帮助jquery中的if/else,jquery,Jquery,这里的代码很简单。我正在检查带有#setactionsuccess的节是否存在,如果不存在,我要创建它并将其预先添加到容器中。第一部分工作顺利,但它从来没有去'其他'(和条件得到满足-我三重检查)。非常感谢您的帮助 if ($('#setactionsuccess')){ var currenttime = new Date(); $('#setactionsuccess').empty(); $('#setactionsuccess').html('<h2>
if ($('#setactionsuccess')){
var currenttime = new Date();
$('#setactionsuccess').empty();
$('#setactionsuccess').html('<h2>Set successfully deleted</h2><p>Set was successfully removed from this Edition on '+currenttime+'</p>');
} else {
console.log('here');
var string = '<section class="successful" id="setactionsuccess"><h2>Set successfully deleted</h2><p>Set was successfully removed from this Edition on '+currenttime+'</p></section>';
console.log(string);
$('#currentsets').prepend(string);
}
if($('setactionsuccess')){
var currenttime=新日期();
$('#setactionsuccess').empty();
$('#setactionsuccess').html('Set已成功删除Set已于'+currenttime+''成功从此版本中删除);
}否则{
console.log('here');
var string='Set已成功删除Set已于'+currenttime+''成功从此版本中删除;
console.log(字符串);
$('#currentsets')。前缀(字符串);
}
试试这个
if($('#setactionsuccess').length > 0)
试试这个
if($('#setactionsuccess').length > 0)
即使没有匹配项,您也总是可以从选择器中获得一些信息。您必须对照0检查匹配数。即使没有匹配项,您也总能从选择器中获得一些信息。您必须根据0检查匹配的数量。您可以使用
长度检查元素是否存在。
if($('#setactionsuccess').length) {
...
}
您可以使用
length
if($('#setactionsuccess').length) {
...
}
if($('#setactionsuccess'))
始终返回true因为javascript检查
null
和undefined
,而jquery永远不会返回null
或undefined
d,即使存在id为setactionsuccess的任何元素尝试查找选择器的长度($('#setactionsuccess')。如果($('#setactionsuccess'))始终返回true,则长度
if($('#setactionsuccess').length) {
...
}
因为javascript检查null
和undefined
,而jquery永远不会返回null
或undefined
d,即使存在id为setactionsuccess的任何元素
尝试查找选择器$(“#setactionsuccess”)的长度。如果始终有id为#setactionsuccess
的块,并且需要检查该块是否为空,则该长度为正确的解决方案:
if($('#setactionsuccess').length) {
...
}
if ($('#setactionsuccess'.text().length != 0)){
var currenttime = new Date();
$('#setactionsuccess').empty();
$('#setactionsuccess').html('<h2>Set successfully deleted</h2><p>Set was successfully removed from this Edition on '+currenttime+'</p>');
} else {
console.log('here');
var string = '<section class="successful" id="setactionsuccess"><h2>Set successfully deleted</h2><p>Set was successfully removed from this Edition on '+currenttime+'</p></section>';
console.log(string);
$('#currentsets').prepend(string);
}
if($('#setactionsuccess'.text().length!=0)){
var currenttime=新日期();
$('#setactionsuccess').empty();
$('#setactionsuccess').html('Set已成功删除Set已于'+currenttime+''成功从此版本中删除);
}否则{
console.log('here');
var string='Set已成功删除Set已于'+currenttime+''成功从此版本中删除;
console.log(字符串);
$('#currentsets')。前缀(字符串);
}
如果始终有id为#setactionsuccess
的块,并且需要检查它是否为空,这是正确的解决方案:
if ($('#setactionsuccess'.text().length != 0)){
var currenttime = new Date();
$('#setactionsuccess').empty();
$('#setactionsuccess').html('<h2>Set successfully deleted</h2><p>Set was successfully removed from this Edition on '+currenttime+'</p>');
} else {
console.log('here');
var string = '<section class="successful" id="setactionsuccess"><h2>Set successfully deleted</h2><p>Set was successfully removed from this Edition on '+currenttime+'</p></section>';
console.log(string);
$('#currentsets').prepend(string);
}
if($('#setactionsuccess'.text().length!=0)){
var currenttime=新日期();
$('#setactionsuccess').empty();
$('#setactionsuccess').html('Set已成功删除Set已于'+currenttime+''成功从此版本中删除);
}否则{
console.log('here');
var string='Set已成功删除Set已于'+currenttime+''成功从此版本中删除;
console.log(字符串);
$('#currentsets')。前缀(字符串);
}
你可以用这个代码检查
您可以通过此代码进行检查。非常感谢!明亮的非常感谢你!明亮的谢谢你的详细解释。这很有用,谢谢你的详细解释。知道这一点很有用