Suppose I have an HTML document that looks like this:
假设我有一个HTML文档,如下所示:
<html lang="en">
...
</html>
My question is: how to get the value of the attribute lang
with jQuery?
I've tried $("html").attr("lang")
but it did not work... any suggestions?
我的问题是:如何使用jQuery获取属性lang的值?我试过$(“html”)。attr(“lang”)但它没有用......有什么建议吗?
3 个解决方案
#1
16
Access the attribute directly, ex :
直接访问该属性,例如:
$('html')[0].lang
#2
5
Use .attr()
使用.attr()
alert($('html').attr('lang'));
警报($( 'HTML')ATTR( '郎'));
#3
5
You don't need to use jQuery for this.
你不需要为此使用jQuery。
The easiest way to retrieve the lang
attribute is to access the lang
property on the read-only documentElement
object:
检索lang属性的最简单方法是访问只读documentElement对象的lang属性:
document.documentElement.lang;
#1
16
Access the attribute directly, ex :
直接访问该属性,例如:
$('html')[0].lang
#2
5
Use .attr()
使用.attr()
alert($('html').attr('lang'));
警报($( 'HTML')ATTR( '郎'));
#3
5
You don't need to use jQuery for this.
你不需要为此使用jQuery。
The easiest way to retrieve the lang
attribute is to access the lang
property on the read-only documentElement
object:
检索lang属性的最简单方法是访问只读documentElement对象的lang属性:
document.documentElement.lang;