Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/452.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_Html - Fatal编程技术网

Javascript:如果内部标签为空,如何隐藏div

Javascript:如果内部标签为空,如何隐藏div,javascript,html,Javascript,Html,如果里面的标签没有任何显示内容,我想用class=“form group”隐藏div 以下是我的html代码: echo'<div class="form-group">'; echo'<label for="exampleInputFile" class="tbh">'.$query2['req_1'].'</label>'; echo'<input typ

如果里面的标签没有任何显示内容,我想用class=“form group”隐藏div 以下是我的html代码:

echo'<div class="form-group">';
                        echo'<label for="exampleInputFile" class="tbh">'.$query2['req_1'].'</label>';
                        echo'<input type="file" name="my_image" accept="image/*" id="exampleInputFile"> ';
                        echo'</div>';
echo';
回显'.$query2['req_1'].';
回声';
回声';
我的javascript在这里:

<script type="text/javascript">
                        if($('label.tbh:empty')){
                            $('div.form-group').hide();
                        }
                        </script>

if($('label.tbh:empty')){
$('div.form-group').hide();
}
还有别的办法吗?在我的代码中,它似乎不起作用。 提前感谢您的帮助。

试试这个:

if($('label.tbh').html() == ""){
     $('div.form-group').hide();
  }
试试这个:

if($('label.tbh').html() == ""){
     $('div.form-group').hide();
  }

您可以这样做:

/* Be sure that your dom is loaded */    
$( document ).ready(function() {
    /* Checking that the label is empty */
    if($("label").html().length == 0) {
        $('div.form-group').hide();   
    }
});
if(strlen($query2['req_1']) == 0)
{
    echo'<div class="form-group">';
    echo'<label for="exampleInputFile" class="tbh">'.$query2["req_1"].'</label>';
    echo'<input type="file" name="my_image" accept="image/*" id="exampleInputFile"> ';
    echo'</div>';
}

您也可以使用
size()
而不是
length

您可以这样做:

/* Be sure that your dom is loaded */    
$( document ).ready(function() {
    /* Checking that the label is empty */
    if($("label").html().length == 0) {
        $('div.form-group').hide();   
    }
});
if(strlen($query2['req_1']) == 0)
{
    echo'<div class="form-group">';
    echo'<label for="exampleInputFile" class="tbh">'.$query2["req_1"].'</label>';
    echo'<input type="file" name="my_image" accept="image/*" id="exampleInputFile"> ';
    echo'</div>';
}
您也可以使用
size()
而不是
length

使用
。text()
是一种更安全的选择。如果标签为空,
.text()
将返回一个空字符串,对其求反将得到
true
。请参阅下面带有示例的交互式代码段。我将相关代码放在一个按钮点击处理程序中,以证明在点击它之后它可以工作

$(“按钮”)。单击(函数(){
如果(!$('.tbh').text()){
$('.form group').hide();
}
});

单击以使用
隐藏。text()
是一个更安全的选项。如果标签为空,
.text()
将返回一个空字符串,对其求反将得到
true
。请参阅下面带有示例的交互式代码段。我将相关代码放在一个按钮点击处理程序中,以证明在点击它之后它可以工作

$(“按钮”)。单击(函数(){
如果(!$('.tbh').text()){
$('.form group').hide();
}
});


单击隐藏
如果您正在使用jQuery,您可以通过以下方式进行隐藏:

$("div.form-group label.tbh:empty").parent().hide();
这是在内部
文档中执行的正确方法。准备好
回调:

$(document).ready(function(){
    $("div.form-group label.tbh:empty").parent().hide()
});
要包括jQuery,请添加

echo '<script src="https://code.jquery.com/jquery-3.1.1.js"></script>';
echo';
到你剧本的开头

但看起来您在后端使用PHP或类似的东西? 如果是这样,您可以在服务器端代码上这样做:

/* Be sure that your dom is loaded */    
$( document ).ready(function() {
    /* Checking that the label is empty */
    if($("label").html().length == 0) {
        $('div.form-group').hide();   
    }
});
if(strlen($query2['req_1']) == 0)
{
    echo'<div class="form-group">';
    echo'<label for="exampleInputFile" class="tbh">'.$query2["req_1"].'</label>';
    echo'<input type="file" name="my_image" accept="image/*" id="exampleInputFile"> ';
    echo'</div>';
}
if(strlen($query2['req_1'])==0)
{
回声';
回显“.$query2[“请求1]”;
回声';
回声';
}

在这种情况下,您不会将不需要的数据传输到客户端。

如果您使用的是jQuery,您可以通过以下方式完成:

$("div.form-group label.tbh:empty").parent().hide();
这是在内部
文档中执行的正确方法。准备好
回调:

$(document).ready(function(){
    $("div.form-group label.tbh:empty").parent().hide()
});
要包括jQuery,请添加

echo '<script src="https://code.jquery.com/jquery-3.1.1.js"></script>';
echo';
到你剧本的开头

但看起来您在后端使用PHP或类似的东西? 如果是这样,您可以在服务器端代码上这样做:

/* Be sure that your dom is loaded */    
$( document ).ready(function() {
    /* Checking that the label is empty */
    if($("label").html().length == 0) {
        $('div.form-group').hide();   
    }
});
if(strlen($query2['req_1']) == 0)
{
    echo'<div class="form-group">';
    echo'<label for="exampleInputFile" class="tbh">'.$query2["req_1"].'</label>';
    echo'<input type="file" name="my_image" accept="image/*" id="exampleInputFile"> ';
    echo'</div>';
}
if(strlen($query2['req_1'])==0)
{
回声';
回显“.$query2[“请求1]”;
回声';
回声';
}

在这种情况下,您不会将不需要的数据传输到客户端。

我给出了一个控制台错误,上面写着:未捕获的ReferenceError:$未定义。如果您得到了未定义的jQuery,那么检查是否加载了正确版本的jQuery?我给出了一个控制台错误,上面写着:未捕获的ReferenceError:$未定义安装$now如果您得到的jQuery没有定义,那么请检查是否加载了正确版本的jQuery?谢谢,它可以工作,但是如果我想自动隐藏它,而不需要按钮,该怎么办?我将它放在一个按钮中,以证明在您在示例中单击它后它可以工作。你只需在你的用例中使用按钮函数中的代码就可以了。谢谢,伙计,我要玩一下+1您的答案对观众很有用。谢谢,它可以工作,但是如果我想自动隐藏它,而不需要任何按钮,该怎么办?我将它放在一个按钮中,以证明您在示例中单击它后它可以工作。你只需在你的用例中使用按钮函数中的代码就可以了。谢谢,伙计,我要玩一下+1您的回答对观众很有用。谢谢@MobDev,jQuery运行得非常好。还有PHP,但PHP相当棘手。这真的帮了大忙,非常感谢。很高兴帮助你:)谢谢@MobDev,jQuery工作得很好。还有PHP,但PHP相当棘手。很高兴能帮助你:)所有的答案不仅对我有用,对观众也很有用+向所有回答的人致意。谢谢你的帮助。所有的答案不仅对我有用,对观众也很有用+向所有回答的人致意。我感谢你的帮助。