Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/84.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
Jquery 根据内容隐藏html节点中的某些元素_Jquery_Each_Show Hide - Fatal编程技术网

Jquery 根据内容隐藏html节点中的某些元素

Jquery 根据内容隐藏html节点中的某些元素,jquery,each,show-hide,Jquery,Each,Show Hide,我想隐藏具有某些特定值的div的一些childrend元素: 假设我们有这样的结构: <div classe="parent" > <div class="child" > <img class="image" src=""> <div class="class1"> <a href="#" class="calsse2">someLink</a> <di

我想隐藏具有某些特定值的div的一些childrend元素: 假设我们有这样的结构:

<div classe="parent" >
    <div class="child" >
    <img class="image" src="">
        <div class="class1">
        <a href="#" class="calsse2">someLink</a>
        <div class="class3">myValue</div>
    </div>
    </div>
</div>
它不太好用

var searchValue=500;
$('.parent').find('.class3').show().filter(函数(){
return+$.trim($(this).text())!==搜索值//或“”
}).hide();
顺便说一句,你这里有一个输入错误:

<div classe="parent" > 

必须是:

<div class="parent" >

试试这个:

$(div:contains("myValue")).filter(:parent).hide();
“其中myValue为int,大于或小于给定值”

下面隐藏了所有子元素,这些子元素的
class3
文本的数字大于
val
中指定的数字:

// hide elements containing a number greater than val
var val = 20;  
$(".parent .child").show().filter(function () {
    var n = $(this).find(".class3").text();
    return +n > val;
}).hide();
演示:

这将查找所有
.child
元素,使它们最初都可见,然后将列表过滤回具有适当值的元素并隐藏它们。与问题中的代码相比,关键区别在于必须使用
.find()
(或其他一些DOM遍历方法)仅检查循环或筛选器中当前
子元素的
class3
元素

上面没有显式测试int,但只会隐藏数值大于
val
的int,因为如果值不是数字,一元加号运算符将返回
NaN
,而
NaN>val
将始终为
false
。如果您确实希望测试int,以便忽略带小数点的数字(它们的div不隐藏),可以执行以下操作:

    return !/\D/.test(n) && +n > val;

也就是说,在对照
val

1测试div之前,先测试div中是否没有非数字字符。为什么要解析文本字段-myValue不是数字?2. $(“.parent”)。我认为每个人都错了。Correct必须是$('.parent').children()。each-Correct???我真的不理解他的问题。当元素class3包含一个特定的值:myValue时,我需要使其不可见
// hide elements containing a number greater than val
var val = 20;  
$(".parent .child").show().filter(function () {
    var n = $(this).find(".class3").text();
    return +n > val;
}).hide();
    return !/\D/.test(n) && +n > val;