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

Kevin 杂记 984 次浏览 35条评论

有时去除网页中的一些动态生成元素,如果没法在后端解决,只能使用 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/

  13. #13

    levalbuterol nebulizer treatment https://amstyles.com/

  14. #14

    dapoxetine patent expiration month and year https://ddapoxetine.com/

  15. #15

    medication without prior prescription https://medpills.bee-rich.com/

  16. #16

    medication without prior prescription https://edmeds.buszcentrum.com/

  17. #17

    hydroxychloroquine sulfate 200 mg cost https://hydroxychloroquinee.com/

  18. #18

    prednisone 10 milligram https://bvsinfotech.com/

  19. #19

    Really enjoyed this blog post. Really looking forward to read more. Want more. Danika Dru Agrippina

  20. #20

    The post is absolutely fantastic! Lots of great info and inspiration, both of which we all need! Also like to admire the time and effort you put into your website and detailed info you offer! I will bookmark your website! Lynelle Arni Matthia

  21. #21

    Thanks very much for this . I will sure visit one this weekend . Deena Clair Rusel

  22. #22

    Stunning story there. What happened after? Take care! Mame Conny Wiersma

  23. #23

    You made various good points there. I did a search on the issue and found a good number of folks will consent with your blog. Kare Skelly Naoma

  24. #24

    If some one wishes to be updated with newest technologies after that he must be visit this web page and be up to date everyday.| Hildegaard Matthus Maressa

  25. #25

    After all, we should remember compellingly reintermediate mission-critical potentialities whereas cross functional scenarios. Phosfluorescently re-engineer distributed processes without standardized supply chains. Quickly initiate efficient initiatives without wireless web services. Interactively underwhelm turnkey initiatives before high-payoff relationships. Holisticly restore superior interfaces before flexible technology. Daron Barclay Phylis

  26. #26

    An fascinating discussion is value comment. I believe that it is best to write extra on this matter, it may not be a taboo topic but generally persons are not enough to speak on such topics. To the next. Cheers Gipsy Pepe Arlinda

  27. #27

    Hi, I rewd youyr blogs like every week. Your writing style is witty, keep it up! Raine Gabby Vida

  28. #28

    In auctor lobortis lacus. Cras id dui. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Sed a libero. Donec vitae orci sed dolor rutrum auctor. Bethany Amory Magdalena

  29. #29

    Thanks for the explanation Rich, and for the link to the script. dkrcp seems like a sophisticated general purpose tool; not for the faint of heart but a viable option for those who know their images well. Jacky Chev Coh

  30. #30

    Along with the whole thing that seems to be developing inside this area, a significant percentage of opinions happen to be very exciting. Nevertheless, I beg your pardon, but I can not subscribe to your whole idea, all be it refreshing none the less. It looks to everyone that your remarks are actually not totally justified and in fact you are your self not fully certain of the point. In any event I did take pleasure in reading through it. Janean Lothaire Duff

  31. #31

    Hi there, I enjoy reading through your article. I like to write a little comment to support you. http://antiibioticsland.com/Tetracycline.htm

  32. #32

    generic cialis 2.5 mg https://cialis.stdstory.com/

  33. #33

    generic cialis 2.5 mg http://cialis.anafassia.com/

  34. #34

    hydroxychloroquine use in military https://hhydroxychloroquine.com/

  35. #35

    Hello There!

    Our team operates an extremely popular Blog. We’ve had a few of our visitors mention your site recently, so I took a look and I’m happy to say that I was really impressed!
    I’m interested in a possible article exchange between your article, https://www.vpsdp.net/javascript-find-element/, and ours as I am sure it will strongly benefit our sites in terms traffic.

    Just so we’re clear, we do this for free, in order to build relationships and to help our friends in the Internet Community.
    If you don’t want to write an article featuring your site on our blog, that’s no problem… maybe we can place a link on one of your articles for a fee.
    Please let me know your thoughts,

    Thanks so much!

    Lora Stonden
    Blogger Outreach Specialist

    P.S. Not sure if you would be interested, but we can provide article writing services for your site as well. Space for this service is extremely limited though, so please let me know if you would like to discuss…

发表评论

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