Best practice for using DOM Level 0 in modern JavaScript applications

后端 未结 5 1410
一整个雨季
一整个雨季 2021-02-01 04:55

Is there an agreed set of \"best practices\" for use of the DOM Level 0 collections in modern JavaScript applications? (document.forms, document.images

5条回答
  •  不知归路
    2021-02-01 05:36

    I'll start by saying that I don't know of any "official" statements like "DOM Level 0 considered harmful". This is just my own opinion.

    I think it depends on the case.

    The issue I have is that it is problematic, especially with dynamically generated/modified documents, to target specific elements using the collections like documents.forms or documents.images. Every time I see someone write document.forms[0], I cringe.

    Current, and even not-so-current browsers provide the ability via functions like getElementsByTagName to simulate the same behaviour, but in a more generic and reusable way. Of course, when you add jQuery into the mix, then there's no reason at all to use those collections.

    The exceptions I have though, are when you are working in a non-jQuery environment and you're accessing things like selectElement.options or tbodyElement.rows. In those cases, it really is simpler when you're doing basic stuff like adding or removing items.

提交回复
热议问题