如何检查嵌套标签的内容是否存在

时间:2016-11-08 07:27:16

标签: javascript jquery node.js

假设我的div

中有这样的结构
    <div class="row">
    <div class="col-sm-10">
        <label for="exampleInputtext1"><a style = "color:black; font-size: 10px;" ><input onchange="submission()" type="checkbox" name="kid1" id="kid1" > </a>Guest 1:</label>
    <input type="text" class="form-control" onblur="submission()"  name="guest1" id="guest1"/>
        </div>
    </div>

第二次我用空值重复这个div

<div class="mainDiv">
    <div class="addrLabel">Address</div>
<div class="addrValue">

<!-- this content value i want to check if exist, then grab it else no value -->

the content which i want to check if exist or not.  if a single value is there i want to grab it also
  <span class="hrefClass">
      <a href="#">next link</a>
  </span>
</div>
<div class="clearfix"></div>

我怎样才能实现这个目标

3 个答案:

答案 0 :(得分:0)

您可以检查.text()是否等于false。像这样:

var addrArray = [];

if($('.addrValue').text() != false)
{
    addrArray.push($(this).text());
}else{
    addrArray.push('no value present');
}

检查小提琴:https://jsfiddle.net/ur5fntvp/

修改
您也可以使用.html()方法代替.text()。这取决于您是否要获取所有HTML标记或仅文本

答案 1 :(得分:0)

$( document ).ready(function() {

 var a  = $('.addrValue').text()
 
 if(a.length) {
   console.log('exists')  
 } else {
   console.log("doesn't exists")  
 }

})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="mainDiv">
    <div class="addrLabel">Address</div>
<div class="addrValue">

<!-- this content value i want to check if exist, then grab it else no value -->

the content which i want to check if exist or not.  if a single value is there i want to grab it also
  <span class="hrefClass">
      <a href="#">next link</a>
  </span>
</div>
<div class="clearfix"></div>

答案 2 :(得分:0)

尝试此演示...它会检查setView() nodeType的{​​{1}}是否为contents()

div.addrValue
3 or a text node