Check if Div with specific Class exists in the page

后端 未结 3 1856
闹比i
闹比i 2021-01-27 20:09

I\'m trying to modify an existing webpage by inserting my own Javascript file in it. I\'m setting a background Image for this webpage by inserting a new Div through my Javascrip

3条回答
  •  时光说笑
    2021-01-27 20:54

    If you load the jQuery library in your page, which you can link from the Google CDN, this is a very simple matter.

    CDN: https://developers.google.com/speed/libraries/devguide#jquery

    Usage: $("div.className").first()

    Otherwise this will require some more effort ... the best possible way to deal with this is add an id attribute to the div element like so:

    And then use the document.getElementById function to retrieve your div tag.

    document.getElementById('myElement');
    

    To see if it has a specific class after this addition please refer to this other post: Test if an element contains a class?

提交回复
热议问题