html - Remove next div with JavaScript, without jquery - Stack Overflow

anyone can help me on a things , I have in a page once <nav class='test'> and below a &

anyone can help me on a things , I have in a page once <nav class='test'> and below a <div> without any class/id, I want to hide/remove <div> element which is in next of <nav> tag , I can do $('.test').next().remove(); but strictly jQuery is not allowed , please give me any trick of JavaScript or CSS, Please note that , i haven’t access to edit any html code only i can add js/css.

Code is looking like this:-

<body class='home'>
    <nav class='test'></nav>
    <div>Want to remove this elements</div>
</body>

Thanks in advance.

anyone can help me on a things , I have in a page once <nav class='test'> and below a <div> without any class/id, I want to hide/remove <div> element which is in next of <nav> tag , I can do $('.test').next().remove(); but strictly jQuery is not allowed , please give me any trick of JavaScript or CSS, Please note that , i haven’t access to edit any html code only i can add js/css.

Code is looking like this:-

<body class='home'>
    <nav class='test'></nav>
    <div>Want to remove this elements</div>
</body>

Thanks in advance.

Share Improve this question edited Jul 16, 2017 at 18:28 ibrahim mahrir 31.7k5 gold badges49 silver badges77 bronze badges asked Jul 16, 2017 at 18:24 Amit mishraAmit mishra 4171 gold badge7 silver badges16 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 6

Use querySelector with css adjacent sibling or next-sibling selector.

var ele = document.querySelector('.test + *')

ele.remove();

// or for older browser
// ele.parentNode.removeChild(ele)
// or
// document.body.removeChild(ele)
<body class='home'>
  <nav class='test'></nav>
  <div>Want to remove this elements</div>
</body>

You can use the nextElementSibling:

let el = document.querySelector('nav').nextElementSibling;
document.body.removeChild(el);
<body class='home'>
    <nav class='test'>d</nav>
    <div>Want to remove this elements</div>
</body>

You can hide it with CSS by selecting the adjacent sibling selector (+), and setting it's display property to none:

.test + div {
  display: none;
}
<body class='home'>
    <nav class='test'>Nav</nav>
    <div>Want to remove this elements</div>
</body>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744301228a4567510.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信