javascript:读取 html 标签中的 lang 属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5031531/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
javascript: reading lang attribute in html tag
提问by HymanLeo
In <html>
tag I have attribute lang
. How do I reach it using .js. I want to use contains of lang as a variable. So as I understand it should start with var lang = ???
?
在<html>
标签中我有属性lang
。我如何使用 .js 访问它。我想使用包含的 lang 作为变量。所以据我所知,它应该以var lang = ???
?
回答by Barrie Reader
In jQuery:
在 jQuery 中:
var theLanguage = $('html').attr('lang');
alert(theLanguage);
If you wanna fiddle: http://jsfiddle.net/NX367/
如果你想摆弄:http: //jsfiddle.net/NX367/
If you want to do it in plain Javascript, this site will explain better than I:
http://www.javascriptkit.com/dhtmltutors/domattribute.shtml
如果你想用纯 Javascript 来做,这个网站会比我解释得更好:http:
//www.javascriptkit.com/dhtmltutors/domattribute.shtml
回答by Loktar
Pure js way
纯js方式
var lang = document.getElementsByTagName("html")[0].getAttribute("lang");
回答by Josh Crozier
The easiest way to retrieve the lang
attribute is to access the lang
property on the read-only documentElement
propertyon the document
object:
检索lang
属性的最简单方法是访问对象lang
的只读documentElement
属性上的document
属性:
document.documentElement.lang;