As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center作为指导。
                            
                        
                    
                
                                7年前关闭。
            
                    
许多ajax调用意味着您正在从不同的模板视图获取内容。在这种情况下,某些ID可能会重复。所以我想知道最佳逻辑如何命名样式规则。
我目前正在决定两种选择:


为每个ID添加一个前缀。每个选择器将看起来像#templateName-idName
将每个模板包装到具有模板名称ID的div中。因此,每个选择器都将以#templateName #idName开头


您对此有何看法?用绝对前缀分隔每个id是个好主意吗?

最佳答案

使用某种形式的psuedo名称空间(例如ID前缀)是防止冲突的好主意。从CSS的角度来看,您应该尝试创建更多可重用/一致的样式规则,因此,除了站点结构元素和一个临时解决方案之外,通常应避免将id用于其他任何事情。显式的类/ id标记和组合器选择器的组合应导致更清晰和更可维护的样式表。就ID而言,由于最初的命名空间原因,使组合的#container-child约定更可取,并且还强调并利用了id的唯一性。

09-25 18:14
查看更多