Javascript:动态添加属性到< html />标签 [英] Javascript: Adding attribute dynamically to the <html /> tag
问题描述
我正在尝试将FB xmlns属性添加到文档的< html>
标签(< html lang =enxml: lang =enxmlns =http://www.w3.org/1999/xhtml>
)。由于某些原因,如下所示添加它不起作用:
htmltag = document.getElementsByTagName('html');
pre>
htmltag [0] .setAttribute(xmlns:fb,http://www.facebook.com/2008/fbml););
我该怎么做?
谢谢!
更新:没有jquery或其他lib可用。
解决方案这可能不是你想要的答案:)
使用客户端代码处理语义标记并不是很好的做法,它隐藏了大多数机器的有价值的机器可读信息,HTML已被获取,解析并在JavaScript执行*时显示*美元短暂和一天迟到!
如果您可以在服务器上添加属性,然后将其发送到浏览器,
如果您有在客户端执行,不要被像jQuery或其他任何框架那样诱惑,那就是一个小任务的巨大开销。
*非科学测试
I'm trying to add FB xmlns attribute to the document's
<html>
tag (<html lang="en" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
) dynamically. For some reason adding it like below does not work:htmltag = document.getElementsByTagName ('html'); htmltag[0].setAttribute("xmlns:fb","http://www.facebook.com/2008/fbml");");
How can I do it?
Thanks!
Update: No jquery or other lib is available.
解决方案This probably isn't the answer you're wanting :)
Tacking on semantic markup with client-side code is not good practice. It hides the valuable machine-readable info from most machines. The HTML is already fetched, parsed and displayed by the time Javascript executes*. A dollar short and a day late!
If you can add the attributes on the server, before it is sent to the browser, go for it.
If you have to do it client-side; don't be tempted by a framework like jQuery or whatever. That's a huge overhead for a small task.
*non-scientific tests
这篇关于Javascript:动态添加属性到< html />标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!