Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
设置<;html>;使用纯JavaScript标记_Javascript_Html - Fatal编程技术网

设置<;html>;使用纯JavaScript标记

设置<;html>;使用纯JavaScript标记,javascript,html,Javascript,Html,我想为标记设置两个属性,它们是: 目录:rtl 朗:啊 我试过: document.documentElement.outerHTML.setAttribute("dir", "rtl"); document.documentElement.outerHTML.setAttribute("lang", "ar"); 以及: 但这两种方法都不奏效 我想这样做是因为我正在使用JavaScript为我的网站开发一个多语言(阿拉伯语和英语)系统。。因此,如果选择的语言是阿拉伯语,我

我想为
标记设置两个属性,它们是:

  • 目录:rtl
  • 朗:啊
我试过:

    document.documentElement.outerHTML.setAttribute("dir", "rtl");
    document.documentElement.outerHTML.setAttribute("lang", "ar");
以及:

但这两种方法都不奏效

我想这样做是因为我正在使用JavaScript为我的网站开发一个多语言(阿拉伯语和英语)系统。。因此,如果选择的语言是阿拉伯语,我想设置这两个属性。

从第一个示例中删除“outerHTML”内容:

document.documentElement.setAttribute("dir", "rtl");
document.documentElement.setAttribute("lang", "ar");
甚至更短:

document.documentElement.dir = "rtl";
document.documentElement.lang = "ar";

为什么不直接使用
querySelector

const html = document.querySelector('html')
html.setAttribute("dir", "rtl");
html.setAttribute("lang", "ar");

只需从第一个示例中删除
outerHTML
<代码>文档。文档元素指的是
元素:

console.log(document.documentElement)//在设置属性之前
document.documentElement.setAttribute(“dir”、“rtl”);
document.documentElement.setAttribute(“lang”、“ar”);
console.log(document.documentElement)//设置属性后

@Nisarg谢谢你找到了。更正。
const html = document.querySelector('html')
html.setAttribute("dir", "rtl");
html.setAttribute("lang", "ar");