HTMLElement.lang property gets or sets the base language of an element's attribute values and text content.

The language code returned by this property is defined in the Tags for Identifying Languages (BCP47) IETF document. Common examples include "en" for English, "ja" for Japanese, "es" for Spanish and so on. The default value of this attribute is 未知 . Note that this attribute, though valid at the individual element level described here, is most often specified for the root element of the document.

This also only works with the lang attribute and not with xml:lang .

句法

var languageUsed = elementNodeReference.lang; // Get the value of lang
elementNodeReference.lang = NewLanguage; // Set new value for lang
					

languageUsed is a string variable that gets the language in which the text of the current element is written. NewLanguage is a string variable with its value setting the language in which the text of the current element is written.

范例

// this snippet compares the base language and
// redirects to another url based on language
if (document.documentElement.lang === "en") {
  window.location.href = "Some_document.html.en";
} else if (document.documentElement.lang === "ru") {
  window.location.href = "Some_document.html.ru";
}
					

规范

规范 状态 注释
DOM (文档对象模型) 2 级 HTML 规范
The definition of 'lang' in that specification.
过时

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
lang Chrome 1 Edge 12 Firefox 1 IE ? Opera Yes Safari 6 WebView Android 4.4 Chrome Android 18 Firefox Android 4 Opera Android Yes Safari iOS Yes Samsung Internet Android 1.0

图例

完整支持

完整支持

兼容性未知 ?

兼容性未知

元数据

  • 最后修改:
  1. HTMLElement
  2. 特性
    1. contentEditable
    2. contextMenu
    3. dir
    4. hidden
    5. innerText
    6. isContentEditable
    7. lang
    8. offsetHeight
    9. offsetLeft
    10. offsetParent
    11. offsetTop
    12. offsetWidth
    13. onabort
    14. onanimationcancel
    15. onanimationend
    16. onanimationiteration
    17. onauxclick
    18. onblur
    19. oncancel
    20. oncanplay
    21. oncanplaythrough
    22. onchange
    23. onclick
    24. onclose
    25. oncontextmenu
    26. oncopy
    27. oncuechange
    28. oncut
    29. ondblclick
    30. ondurationchange
    31. onended
    32. onerror
    33. onfocus
    34. onformdata
    35. ongotpointercapture
    36. oninput
    37. oninvalid
    38. onkeydown
    39. onkeypress
    40. onkeyup
    41. onload
    42. onloadeddata
    43. onloadedmetadata
    44. onloadend
    45. onloadstart
    46. onlostpointercapture
    47. onmousedown
    48. onmouseenter
    49. onmouseleave
    50. onmousemove
    51. onmouseout
    52. onmouseover
    53. onmouseup
    54. onpaste
    55. onpause
    56. onplay
    57. onplaying
    58. onpointercancel
    59. onpointerdown
    60. onpointerenter
    61. onpointerleave
    62. onpointermove
    63. onpointerout
    64. onpointerover
    65. onpointerup
    66. onreset
    67. onresize
    68. onscroll
    69. onselect
    70. onselectionchange
    71. onselectstart
    72. onsubmit
    73. ontouchcancel
    74. ontouchstart
    75. ontransitioncancel
    76. ontransitionend
    77. onwheel
    78. outerText
    79. title
  3. 方法
    1. click()
    2. forceSpellCheck()
  4. 事件
    1. animationcancel
    2. animationend
    3. animationiteration
    4. animationstart
    5. beforeinput
    6. change
    7. gotpointercapture
    8. input
    9. lostpointercapture
    10. pointercancel
    11. pointerdown
    12. pointerenter
    13. pointerleave
    14. pointermove
    15. pointerout
    16. pointerover
    17. pointerup
    18. transitioncancel
    19. transitionend
    20. transitionrun
    21. transitionstart
  5. 继承:
    1. 元素
    2. 节点
    3. EventTarget
  6. HTML DOM 相关页面
    1. BeforeUnloadEvent
    2. DOMStringMap
    3. ErrorEvent
    4. GlobalEventHandlers
    5. HTMLAnchorElement
    6. HTMLAreaElement
    7. HTMLAudioElement
    8. HTMLBRElement
    9. HTMLBaseElement
    10. HTMLBaseFontElement
    11. HTMLBodyElement
    12. HTMLButtonElement
    13. HTMLCanvasElement
    14. HTMLContentElement
    15. HTMLDListElement
    16. HTMLDataElement
    17. HTMLDataListElement
    18. HTMLDialogElement
    19. HTMLDivElement
    20. HTMLDocument
    21. HTMLEmbedElement
    22. HTMLFieldSetElement
    23. HTMLFormControlsCollection
    24. HTMLFormElement
    25. HTMLFrameSetElement
    26. HTMLHRElement
    27. HTMLHeadElement
    28. HTMLHeadingElement
    29. HTMLHtmlElement
    30. HTMLIFrameElement
    31. HTMLImageElement
    32. HTMLInputElement
    33. HTMLIsIndexElement
    34. HTMLKeygenElement
    35. HTMLLIElement
    36. HTMLLabelElement
    37. HTMLLegendElement
    38. HTMLLinkElement
    39. HTMLMapElement
    40. HTMLMediaElement
    41. HTMLMetaElement
    42. HTMLMeterElement
    43. HTMLModElement
    44. HTMLOListElement
    45. HTMLObjectElement
    46. HTMLOptGroupElement
    47. HTMLOptionElement
    48. HTMLOptionsCollection
    49. HTMLOutputElement
    50. HTMLParagraphElement
    51. HTMLParamElement
    52. HTMLPictureElement
    53. HTMLPreElement
    54. HTMLProgressElement
    55. HTMLQuoteElement
    56. HTMLScriptElement
    57. HTMLSelectElement
    58. HTMLShadowElement
    59. HTMLSourceElement
    60. HTMLSpanElement
    61. HTMLStyleElement
    62. HTMLTableCaptionElement
    63. HTMLTableCellElement
    64. HTMLTableColElement
    65. HTMLTableDataCellElement
    66. HTMLTableElement
    67. HTMLTableHeaderCellElement
    68. HTMLTableRowElement
    69. HTMLTableSectionElement
    70. HTMLTemplateElement
    71. HTMLTextAreaElement
    72. HTMLTimeElement
    73. HTMLTitleElement
    74. HTMLTrackElement
    75. HTMLUListElement
    76. HTMLUnknownElement
    77. HTMLVideoElement
    78. HashChangeEvent
    79. 历史
    80. ImageData
    81. 定位
    82. MessageChannel
    83. MessageEvent
    84. MessagePort
    85. Navigator
    86. NavigatorGeolocation
    87. NavigatorID
    88. NavigatorLanguage
    89. NavigatorOnLine
    90. NavigatorPlugins
    91. PageTransitionEvent
    92. Plugin
    93. PluginArray
    94. PopStateEvent
    95. PortCollection
    96. PromiseRejectionEvent
    97. RadioNodeList
    98. Transferable
    99. ValidityState
    100. Window
    101. WindowBase64
    102. WindowEventHandlers
    103. WindowTimers