Is it normal to have two elements with same id in

2020-02-10 14:25发布

i know, that two elements can't hav the same id. But it's happens so, that in my project i have two elements with same id in other divs, like this

<div id="div1">
     <img id="loading" />
</div>
<div id="div2">
     <img id="loading" />
</div>

and css:

#div1 #loading
{
    some style here...
}
#div2 #loading
{
    another style here...
}

works fine for me, but maybe it is not reccomended to do by so?

Thanks

UPDATE

Yes, i know, thet i can use classes, and it's strongly recomended to do by so, but i want to know is there any potential risk in this usage of id? i think no, becouse when i wrote for example

$("#div1 #loading")... it becomes a unique element. isn't it?

Final Update

Use suggested principe, if you have reasons to do that! ;)

13条回答
Rolldiameter
2楼-- · 2020-02-10 15:23

Unique:

In mathematics and logic, the phrase "there is one and only one" is used to indicate that exactly one object with a certain property exists.

#div1 #loading does not remedy the fact that you have two instances of #loading in your document. So, no, don't do that.

查看更多
登录 后发表回答