2016-11-08 41 views
0

假設我有一個這樣的結構在我的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> 

我第二次是在重複這個div與空值

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

<!-- no value at all --> 
    <span class="hrefClass"> 
     <a href="#">next link</a> 
    </span> 
</div> 
<div class="clearfix"></div> 

我怎樣才能做到這一點

+0

下面的答案沒有解決我的問題,因爲我想檢查是否有一個內容然後只有我想推 – EaB

+0

請提供我的解決方案,如果沒有任何價值,但3回答沒有解決這個 – EaB

+0

這裏是一個小提琴不輸入其他條件推送沒有值存在https://jsfiddle.net/ur5fntvp/8/ – EaB

回答

0

試試這個演示......它檢查的contents()div.addrValuenodeType3 or a text node

var addrArray = []; 
 
$(".addrValue").each(function(ndx, elem) { 
 
    var text = $(elem).contents() 
 
    .filter(function() { 
 
     if (this.nodeType === 3) { // chech for text node 
 
     if ($(this).text().trim().length !== 0) { 
 
      return true; 
 
     } 
 
     } 
 
    }).text(); 
 
    if (text.length === 0) { // check string length 
 
    addrArray.push('no value at all'); 
 
    } else { 
 
    addrArray.push(text.trim()); 
 
    } 
 

 
}); 
 

 
console.log(addrArray)
<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> 
 
</div> 
 

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

 

 
    <span class="hrefClass"> 
 
     <a href="#">next link</a> 
 
    </span> 
 
    </div> 
 
    <div class="clearfix"></div> 
 
</div>

+0

非常感謝非常靈活的答案:) – EaB

+0

很高興它有幫助。 – four

1

您可以檢查.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標籤或純文本

+0

如果沒有值,但我的代碼無法執行,我想推送任何值,這裏是一個小提琴https:// jsfiddle.net/ur5fntvp/4/ – EaB

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>