2017-05-29 35 views
2

我使用goog.dom.safeHtmlToNode谷歌关闭图书馆sanitizer.sanitize消除了对DOM元素我的ID

动态地更新与内容标签,因为谷歌的封库的更新版本中删除的DOM片段方法:goog.dom.htmlToDocumentFragment(htmlString)

消毒剂去除从DOM元素我的 “ID = XYZ”。例如:

'<input type="email" id="openid" name="openid" size="20" style="font-size: 18px" />' 

成为

'<input type="email" size="20" style="font-size: 18px;" />' 

我所用的ID来获得内容从这些元素的用户输入。 我如何告诉卫生洗涤剂不要删除这些属性?我曾尝试下面的代码没有成功:

var sanitizer = 
new goog.html.sanitizer.HtmlSanitizer.Builder() 
.withCustomNetworkRequestUrlPolicy(goog.html.SafeUrl.sanitize) 
.allowCssStyles() 
.allowFormTag() 
.addOriginalTagNames() 
.allowDataAttributes([ "data-id","data-name" ]) //data-* attributes are inert as per HTML5 specs 
.build(); 

goog.dom.safeHtmlToNode(sanitizer.sanitize (htmlString)); 

我也曾尝试重新命名我的“ID”到“数据ID”,该字符串仍从消毒输出中删除。

Thank you in advance for your help on this matter. 

回答

1

这改变了令牌政策允许,允许没有全部,而不是。令牌策略用于ID和CLASS属性,所以这应该允许您保留您的ID属性。

.withCustomTokenPolicy(goog.functions.identity) 
+0

谢谢。制定政策的伎俩。我在哪里可以找到关于谷歌封闭库的其他文档,即教程,例子(是的,我知道它附带的演示)。我通常使用测试程序来弄清楚如何调用函数。这将是很好,有更多的信息!无论如何谢谢!它现在工作! – Lillian

+0

主要网站是https://developers.google.com/closure/library/ – ghosttie

0

这里是经由online closure compiler

我只是说“id”和“数据ID”作为.allowDataAttributes([ "id","data-id" ])值的测试用例的例子。

如果编译“高级模式”的例子,结果粘贴到一个控制台,您会看到“ID”和“数据ID”的存在。

+0

我在运行时出现断言错误。我查看了代码,此函数需要前缀“data-”。谢谢,尽管......每个提示都有帮助! – Lillian

相关问题