开发者

javascript: reading lang attribute in html tag

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 = ????


In jQuery:

    var theLanguage = $('html').attr('lang');
    alert(theLanguage);

If you wanna fiddle: 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


Pure js way

var lang = document.getElementsByTagName("html")[0].getAttribute("lang");

http://jsfiddle.net/loktar/ZRvE6/


The easiest way to retrieve the lang attribute is to access the lang property on the read-only documentElement property on the document object:

document.documentElement.lang;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜