How to set HTML lang attribute dynamically?

后端 未结 3 1738
小鲜肉
小鲜肉 2020-12-18 23:48

How to set HTML \'lang\' attribute dynamically in a web application?

I tried using jQuery as follows to insert the \'lang\' attribute:

相关标签:
3条回答
  • 2020-12-19 00:08

    document.documentElement.setAttribute('lang', navigator.language);

    or

    document.documentElement.lang = navigator.language;

    0 讨论(0)
  • 2020-12-19 00:08

    But if you see generated source (View Source), 'lang' isn't getting set at all.

    View Source doesn't show you the generated source. It shows you the real source. If you want to change it, then you need to change it on the server before you deliver it to the browser. You can't modify it with client side JavaScript.

    Your DOM changes will show up in the live DOM, which will be visible through Inspect Element.

    0 讨论(0)
  • 2020-12-19 00:26

    Can't comment yet.

    I tried the snippet and it worked for me. Of course I added a string value instead of your 'language' variable.

    Don't forget to import jQuery in the top menu in order for it to work in the snippet.

    0 讨论(0)
提交回复
热议问题