Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/86.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 jQuery:选择祖父母';s h1标签_Javascript_Jquery_Html_Css - Fatal编程技术网

Javascript jQuery:选择祖父母';s h1标签

Javascript jQuery:选择祖父母';s h1标签,javascript,jquery,html,css,Javascript,Jquery,Html,Css,我在一个非常严格的框架(NetSuite)中工作,我可以直接控制一个小部分,下面是h3和p文本。结构与此类似: <div class="grandparent"> <h1>Title Text</h1> <div class="otherstuff">Some text</div> <div class="parent"> <h3>Text I have control o

我在一个非常严格的框架(NetSuite)中工作,我可以直接控制一个小部分,下面是h3和p文本。结构与此类似:

<div class="grandparent">
    <h1>Title Text</h1>
    <div class="otherstuff">Some text</div>
    <div class="parent">
        <h3>Text I have control over</h3>
        <p>More text I have control over</p>
    </div>
</div>
和它的变化,但没有任何运气。我研究了.parentUntil()函数,但遇到了同样的问题。我抓取祖先元素没有问题,但在尝试抓取这些祖先元素时遇到了麻烦

有人能帮我吗


编辑:谢谢大家花时间和精力回答我的问题。我真的很感激

我可以想出两个选择器,如果你把
.myclass
放回去,它们可能会起作用

$('.myclass').closest('.grandparent').find('h1').css('display','none');

  • 使用
    closest()
    遍历到祖父母
  • 使用
    find()
    选择所需的元素
  • 您可以使用
    hide()
    代替
    css('display','none')
    ,因为它们是等效的
  • var-grandparent=$('.myclass')。最近('.grandparent');
    find('h1,.otherstuff').hide()
    
    
    标题文本
    一些文本
    我可以控制的文本
    我可以控制更多的文本


    如果标题总是紧跟在带有“otherstuff”类的div之前,那么您可以使用:

    $('.otherstuff').prev('h1').css('display', 'none');
    
    此处的文档:

    您可以使用:

    $('.myclass').closest('.grandparent').find('>h1,>.otherstuff').hide();
    
    用于直接后代元素

    直接控制哪个是h3

    尝试利用
    .parent()
    .sibbines()

    $(“h3”).parent().sibbines().hide();//`$(“.parent”).sides().hide();`
    
    
    标题文本
    一些文本
    我可以控制的文本
    我可以控制更多的文本

    最近的()选择祖先,您需要的是兄弟()

    因此:

    将返回父div的h1同级数组,在本例中,该数组的第一项是h1


    您可以迭代这些元素并隐藏它们(如果有多个)

    其中是
    。myclass
    元素?使用
    。hide()
    而不是
    。css('display','none')
    @Sarwic“直接控制下面的h3和p文本”要求是否允许选择
    h3
    父元素
    .parent
    ,或者只有
    h3
    元素?对不起,没有那么清楚。“.myclass”用于h3标记@guest271314,我可以选择所有内容,但我只能通过管理面板直接更改h3和p文本,其中是
    .myclass
    元素?原始海报上写着“我尝试给h3标签一个类”。它会出现在
    上,太棒了!感谢您的直截了当和清晰的解释。
    .closest()
    是否只沿DOM向上移动,还是也可以向下移动?如果后者是真的,我怎么知道它将向哪个方向移动?@TylerH,不,它只会沿着DOM向上移动,直到找到选择器为止。我也想知道。我做错了什么?
    $('.otherstuff').prev('h1').css('display', 'none');
    
    $('.myclass').closest('.grandparent').find('>h1,>.otherstuff').hide();
    
    $('.your_h3_class').parent().siblings('h1')