Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/440.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 显示依赖于是否包含内容的div元素_Javascript_Jquery_Jquery Selectors - Fatal编程技术网

Javascript 显示依赖于是否包含内容的div元素

Javascript 显示依赖于是否包含内容的div元素,javascript,jquery,jquery-selectors,Javascript,Jquery,Jquery Selectors,使用jQuery,我试图确定中是否有内容,如果有,则我不想做任何事情,如果没有,则我想向其添加显示:无或.hide()。下面是我的想法 if($('#left content:contains(“”)).length只需在选择器中使用 $('#left-content:empty').hide(); 只需在选择器中使用 $('#left-content:empty').hide(); 尝试删除第一个空格: // first remove whitespaces // html ob

使用jQuery,我试图确定
中是否有内容,如果有,则我不想做任何事情,如果没有,则我想向其添加
显示:无
.hide()
。下面是我的想法

if($('#left content:contains(“”)).length只需在选择器中使用

$('#left-content:empty').hide();
只需在选择器中使用

$('#left-content:empty').hide();

尝试删除第一个空格:

  // first remove whitespaces

  // html objects content version:
  var content = $.trim($("#left-content).html()).length;

  if(content == 0) {
     $("#left-content).hide();
  }

  // html RAW content version:
  var content = $.trim($("#left-content).html()); // <-- same but without length

  if(content == "") {                             // <-- this == ""
     $("#left-content).hide();
  }
//首先删除空白
//html对象内容版本:
var content=$.trim($(“#left content).html()).length;
如果(内容==0){
$(“#左内容).hide();
}
//html原始内容版本:

var content=$.trim($(“#left content).html());//尝试删除第一个空格:

  // first remove whitespaces

  // html objects content version:
  var content = $.trim($("#left-content).html()).length;

  if(content == 0) {
     $("#left-content).hide();
  }

  // html RAW content version:
  var content = $.trim($("#left-content).html()); // <-- same but without length

  if(content == "") {                             // <-- this == ""
     $("#left-content).hide();
  }
//首先删除空白
//html对象内容版本:
var content=$.trim($(“#left content).html()).length;
如果(内容==0){
$(“#左内容).hide();
}
//html原始内容版本:

var content=$.trim($(“#left content).html());//在什么情况下
html().length
将小于0?在什么情况下
html().length
将小于0?如果
#left content
中有空白文本节点,这将无法正常工作。例如
(即
在一个空的div中。是的,文档中指出,只有在没有子节点(包括文本节点)的情况下,它才被视为空的。这肯定会让您感到困惑。如果
#左内容中有空格textnodes
。例如
(即
在一个空的div中。是的,文档中指出,只有在没有子节点(包括文本节点)的情况下,才会将其视为空的。这肯定会让您绊倒。