How to access HTML element without ID?

后端 未结 6 1545
暖寄归人
暖寄归人 2020-11-30 05:39

For instance in the snippet below - how do I access the h1 element knowing the ID of parent element (header-inner div)?

&
6条回答
  •  不知归路
    2020-11-30 06:27

    If you were to use jQuery as mentioned by some posters, you can get access to the element very easily like so (though technically this would return a collection of matching elements if there were more than one H1 descendant):

    var element = $('#header-inner h1');
    

    Using a library like JQuery makes things like this trivial compared to the normal ways as mentioned in other posts. Then once you have a reference to it in a jQuery object, you have even more functions available to easily manipulate its content and appearance.

提交回复
热议问题