JavaScript 判断页面有无特定元素再执行操作

Kevin 杂记 786 次浏览 12条评论

有时去除网页中的一些动态生成元素,如果没法在后端解决,只能使用 JavaScript 解决了。下面贴个简单实现代码,先在页面中检查有无 demo1 选择器元素,如果有则执行删除 demo2 元素。根据需要,还可以加更多判断或进行其它操作。

HTM

<p id="demo1">demo1</p>
<p id="demo2">demo2</p>

JavaScript

var pa = document.getElementById("demo1");
if (pa !== null) {
  var pb = document.getElementById("demo2");
  pb.parentNode.removeChild(pb);
}

演示 https://codepen.io/anon/pen/jjGwQr

评论列表

  1. #1

    Hi, very nice website, cheers!
    ——————————————————
    Need cheap and reliable hosting? Our shared plans start at $10 for an year and VPS plans for $6/Mo.
    ——————————————————
    Check here: https://www.reliable-webhosting.com/

  2. #2

    hydroxychloroquine sulfate nz https://hydroxychloroquine.webbfenix.com/

  3. #3

    herbs that act like vidalista https://vidalista.mlsmalta.com/

  4. #4

    goodrx coupons for viagra http://droga5.net/

  5. #5

    ivermectin for head lice https://ivermectin.webbfenix.com/

  6. #6

    hydroxychloroquine coronavirus https://hydroxychloroquine.mlsmalta.com/

  7. #7

    cialis 20 mg how to use http://www.lm360.us/

  8. #8

    ivermectin tablets https://ivermectin.mlsmalta.com/

  9. #9

    super cialis dapoxetine https://dapoxetine.bee-rich.com/

  10. #10

    how much vidalista daily https://vidalista40mg.mlsmalta.com/

  11. #11

    price of cialis at walmart https://wisig.org/

  12. #12

    how does tamoxifen work https://tamoxifen.mrdgeography.com/

发表评论

电邮地址用于 Gravatar 头像显示,不会被公开可见。