Can multiple different HTML elements have the same ID if they're different elements?

后端 未结 16 1854
无人及你
无人及你 2020-11-21 05:30

Can multiple HTML elements have the same ID if they\'re of different element types? Is a scenario like this valid? Eg:

div#foo
span#foo
a#foo
16条回答
  •  情书的邮戳
    2020-11-21 05:48

    We can use class name instead of using id. html id are should be unique but classes are not. when retrieving data using class name can reduce number of code lines in your js files.

    $(document).ready(function ()
    {
        $(".class_name").click(function ()
        {
            //code
        });
    });

提交回复
热议问题