如何检查嵌套标签的内容是否存在
How to check if the content of the nested tags exists or not
假设我在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>
第二次使用null值重复这个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>
如何实现这个
试试这个演示…检查contents()
div.addrValue
的nodeType
是否为3 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>
可以检查.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标签还是只获取文本
$( 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>
相关文章:
- 用于检查数组中是否存在元素的javascript自定义方法
- 是否存在React Native“;WEB代码安全防护”;
- 验证会话中是否存在对象's数组
- 如何查找值是否存在于二叉树中
- 检查是否存在使用chrome扩展的javascript库
- 是否存在Javascript Liferay Service库的文档?如何处理错误情况
- 测试mongo脚本中是否存在参数
- 检查搜索结果是否存在多次如果是,则在Javascript中只显示一个结果
- 检查数组中是否存在字符串值,并返回找到的数组值js
- 如何通过json对象选项卡中的Id来检查对象是否存在
- 检查是否存在任意控制器/操作
- Javascript滑块不滑动,如何判断是否存在JS冲突
- 根据手机上是否存在文件,在jQuery mobile中动态填充列表视图
- javascript测试是否存在两个标志中的任何一个
- 如何使用javascript检查移动sd卡中是否存在文件
- 当提供函数名称时,检查函数是否存在于同一作用域中
- 需要帮助使用JQuery.inArray()检查值是否存在
- 确定元素是否存在
- 使用javascript检查服务器上是否存在html文件
- 是否存在类似ExtJS 4的框架;正常的“;网站