如何检查jquery中的var

| 我有这个脚本:
var display = \'<div class=\"uploadData\" id=\"pxupload\'+ itr +\'_text\">\';
我想检查
diplay
中的
div
是否存在。像这样:
if (display = null){
        $(\"#px_display\").append(display);
    }else if  (display != null) {
        $(config.buttonClear).trigger(\'click\');
    }
我不知道该说法是否正确。
已邀请:
最好的办法是...
var display = \'<div class=\"uploadData\" id=\"pxupload\'+ itr +\'_text\">\';

var id = $(display).attr(\'id\');

if ($(\'#\' + id).length) {
   // Some element with the same id attribute exists.
}
当然,这不太准确。 最好是先设置该变量,就像这样...
var displayId = \'pxupload\' + itr;

if ($(\'#\' + displayId).length) {
   // Some element with the same id attribute exists.
}
由于ID是唯一且可构造的,因此您可以执行以下操作:
if($(\"#pxupload\" + itr + \"_text\") == null){
        $(\"#px_display\").append(display);
}else if  (display != null) {
    $(config.buttonClear).trigger(\'click\');
}

要回复问题请先登录注册