javascript get child by id

后端 未结 4 1247
广开言路
广开言路 2020-12-13 17:23
Test
child

I want to change the style of the child div

4条回答
  •  鱼传尺愫
    2020-12-13 17:48

    In modern browsers (IE8, Firefox, Chrome, Opera, Safari) you can use querySelector():

    function test(el){
      el.querySelector("#child").style.display = "none";
    }
    

    For older browsers (<=IE7), you would have to use some sort of library, such as Sizzle or a framework, such as jQuery, to work with selectors.

    As mentioned, IDs are supposed to be unique within a document, so it's easiest to just use document.getElementById("child").

提交回复
热议问题