aboutsummaryrefslogtreecommitdiff
path: root/files/zh-tw/web/api/htmlelement/lang/index.html
blob: ff546e3ca948b950b1be01ea0dc799637c6bcd72 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
---
title: HTMLElement.lang
slug: Web/API/HTMLElement/lang
tags:
  - API
  - HTML DOM
  - HTMLElement
  - NeedsBrowserCompatibility
  - NeedsUpdate
  - Property
  - Reference
translation_of: Web/API/HTMLElement/lang
---
<div>{{ APIRef("HTML DOM") }}</div>

<p><code><strong>HTMLElement.lang</strong></code> 屬性({{Glossary("property")}})可以讀取或設定一個表示元素之語系的標籤屬性({{Glossary("attribute")}})值。</p>

<p><code>HTMLElement.lang</code> 屬性所回傳的語系代碼定義於網際網路工程任務小組(IETF)的 <a class="external" href="https://www.ietf.org/rfc/bcp/bcp47.txt"><em>Tags for Identifying Languages (BCP47)</em></a> 文件中。常見的例子如 "en" 代表英語、"ja" 代表日語、"es" 代表西班牙語等等。此標籤屬性的預設值為 <code>unknown</code>。請留意,雖然此標籤屬性於個別層級的元素上是有效的,但大部分都設定於文件的根元素。</p>

<p><code>HTMLElement.lang</code> 屬性只對 <code>lang</code> 標籤屬性有作用,而不是 <code>xml:lang</code></p>

<h2 id="Syntax" name="Syntax">語法</h2>

<pre class="syntaxbox">var <var>languageUsed</var> = elementNodeReference.lang; // Get the value of lang
elementNodeReference.lang = <var>NewLanguage</var>; // Set new value for lang
</pre>

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

<h2 id="Example" name="Example">範例</h2>

<pre class="brush: js">// 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";
}</pre>

<h2 id="Specification" name="Specification">規範</h2>

<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>{{SpecName('DOM2 HTML', 'html.html#ID-59132807', 'lang')}}</td>
   <td>{{Spec2('DOM2 HTML')}}</td>
   <td> </td>
  </tr>
 </tbody>
</table>

<p> </p>