Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/70.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 如何基于子元素隐藏父元素';s文本值_Javascript_Jquery - Fatal编程技术网

Javascript 如何基于子元素隐藏父元素';s文本值

Javascript 如何基于子元素隐藏父元素';s文本值,javascript,jquery,Javascript,Jquery,我想要的是能够在DOM中搜索特定字符串。如果文本在页面上,则隐藏某些元素并显示其他元素。catch是在PHP foreach语句中动态给出的id 我尝试搜索特定于文本的.exists()函数。到目前为止,我已经尝试: jQuery(document).ready(function($) { var string1 = 'string1', string2 = 'string2'; if ($('li').children().text() === string1

我想要的是能够在DOM中搜索特定字符串。如果文本在页面上,则隐藏某些元素并显示其他元素。catch是在PHP foreach语句中动态给出的id

我尝试搜索特定于文本的.exists()函数。到目前为止,我已经尝试:

jQuery(document).ready(function($) {
    var string1 = 'string1',
        string2 = 'string2';

    if ($('li').children().text() === string1) {
        if ($('li').children().text() === string2) {
            $(this).parent().show();
        } else {
            $(this).parent().hide();
        }
    } else {
        if ($('li').children().text() === string2) {
            $(this).parent().hide();
        } else {
            $(this).parent().show();
        }
    }
});
所以我要找的是:

1) 搜索string1的页面

2) 如果页面上存在string1,则显示包含string2的子元素的父元素并隐藏任何其他字符串

3) 如果不存在,则隐藏string1和string2


但这不起作用。我在Jquery/JS方面不是最棒的,一般来说我是一个初学者。

看看一个字符串是否包含另一个可以使用的(子)字符串。 并检查它是否为-1

使用
each()
可以循环所有
li
元素。考虑到您的JSFIDLE(在评论中提供),我得到了以下结果:


查看字符串是否包含可以使用的其他(子)字符串。 并检查它是否为-1

使用
each()
可以循环所有
li
元素。考虑到您的JSFIDLE(在评论中提供),我得到了以下结果:


也可以提供您的HTML样本请。但是,您的第一个
if
语句似乎是多余的,因为在这两种情况下,您的逻辑是相同的。html是通过foreach循环动态生成的-我可以添加它,但您将看到的唯一内容是$this->getTitle()等。请描述您想要实现的目标,因为代码中的条件不正确。请参阅修订的问题string1的搜索页面是什么意思?搜索所有文档DOM?还可以提供您的HTML示例。但是,您的第一个
if
语句似乎是多余的,因为在这两种情况下,您的逻辑是相同的。html是通过foreach循环动态生成的-我可以添加它,但您将看到的唯一内容是$this->getTitle()等。请描述您想要实现的目标,因为代码中的条件不正确。请参阅修订的问题string1的搜索页面是什么意思?搜索所有文档DOM?不幸的是,它不起作用:(如果需要,请参阅修订的问题以获取更多信息。您需要在子项上循环。让我更新我的答案,以建议使用.each(),但我认为这可能为我指明了正确的方向。@TT120这是您想要的吗?(注:别忘了在JSFIDLE中包含jQuery)不幸的是,它不起作用:(如果需要,请参阅修订的问题以获取更多信息。您需要在孩子们身上循环。让我更新我的答案,以建议使用.each(),但我想这可能会为我指明正确的方向。@TT120这是您想要的吗?(注意:不要忘记在JSFIDLE中包含jQuery)
jQuery(document).ready(function($) {
    var string1 = 'string1',
        string2 = 'string2';
    var string1exists= $('li').text().indexOf( string1)!==-1;
    console.log(string1exists);

    $('li').each(function () {
         if ($(this).text().indexOf( string2)!==-1 && string1exists==true) {
             // this child contains string 2
            $('li').hide(); //hide all others
            $(this).show();                    
        }
        if ($(this).text().indexOf( string2)!==-1 && string1exists==false) {
             // this child contains string 2
            $(this).hide();                    
        }
        if ($(this).text().indexOf( string1)!==-1 && string1exists==false) {
             // this child contains string 1
            $(this).hide();                    
        }
    });

});