:lang() Selector


lang selector

Description: Selects all elements of the specified language.

  • version added: 1.9jQuery( ":lang(language)" )

    language: A language code.

The :lang() selector matches elements that have a language value equal to the supplied language code or that start with the supplied language code immediately followed by "-". For example, the selector $("div:lang(en)")will match <div lang="en"> and <div lang="en-us"> (and any of their descendant <div>s), but not <div lang="fr">

For HTML elements, the language value is determined by the lang attribute and possibly information from meta elements or HTTP headers.

Further discussion of this usage can be found in the W3C CSS specification.

Example:

Color div elements according to their language.

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
60
61
62
63
64
65
66
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>lang demo</title>
<style>
body {
background-color: #ccc;
}
h3 {
margin: .25em 0;
}
div {
line-height: 1.5em
}
.usa {
background-color: #f00;
color: #fff;
}
.usa .usa {
background-color: #fff;
color: #000;
}
.usa .usa .usa {
background-color: #00f;
color: #fff;
}
.spain {
background-color: #f00;
color: #ff0;
}
.spain .spain {
background-color: #ff0;
color: #f00;
line-height: 3em;
}
.spain .spain .spain {
background-color: #f00;
color: #ff0;
line-height: 1.5em;
}
</style>
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
<h3>USA</h3>
<div lang="en-us">red
<div>white
<div>and blue</div>
</div>
</div>
<h3>EspaƱa</h3>
<div lang="es-es">rojo
<div>amarillo
<div>y rojo</div>
</div>
</div>
<script>
$( "div:lang(en-us)" ).addClass( "usa" );
$( "div:lang(es-es)" ).addClass( "spain" );
</script>
</body>
</html>

Demo: