JQuery basic selector usage and non unique element ID

前端 未结 4 769
别那么骄傲
别那么骄傲 2021-01-22 10:38

I\'am maintaining a GUI built using JQuery. In one part of the GUI, multiple tabs can be opened to edit data.

When a new tab is opened, it is created by cloning the firs

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-22 11:27

    ID's are unique identifiers. The moment you introduce a duplicate id, you have an invalid document on your hands.

    The best way to get around this is to refrain from using id's on anything that is going to be cloned. Instead, use a "unique" class name to identify the element. Then, when it is cloned, you can walk down the DOM to each copy of the class. jQuery has very good DOM traversal methods for this.

    http://api.jquery.com/category/traversing/

    Additionally: .children(), .parent(), .parents(), and .siblings() are particularly useful. I'd stay away from .find() unless it cannot be helped. .find() can be slow if you are searching through many, many nodes in the DOM. Since you're building an interface, this might be the case.

提交回复
热议问题