在IE / FF中使用getElementsByTagName时,自定义标签的处理方式不同
Custom tags handled differently when using getElementsByTagName in IE / FF
我有以下页面:
<html>
<head>
<title>Test</title>
<script type="text/javascript">
function BuildTree() {
var total = 0;
var pages = document.getElementsByTagName('custom:page');
var questions = pages[0].getElementsByTagName('custom:question');
for (var i = 0; i < questions.length; ++i) {
var question = questions[i];
var val = question.getAttribute('value')
total += val;
}
alert("Total: " + total);
};
</script>
</head>
<body>
<custom:pages>
<custom:page>
<custom:question value="1">aaa</custom:question>
<custom:question value="2">bbb</custom:question>
<custom:question value="3">ccc</custom:question>
</custom:page>
<custom:page>
<custom:question value="1">aaa</custom:question>
<custom:question value="2">bbb</custom:question>
<custom:question value="3">ccc</custom:question>
</custom:page>
</custom:pages>
<input id="btnTest" type="button" value="Test" onclick="BuildTree();" />
</body>
</html>
当我在IE中单击"测试"按钮时,结果为0,当我在FF中单击它时,结果为0123。
如何在两个浏览器中获得相同的结果?即"0123"。
请注意,我已将其缩减为尽可能简单的示例。我不能使用jquery或任何第三方库,我需要一个纯粹的Javasscript解决方案。我也无法更改自定义:标签。
谢谢
<html xmlns:custom="http://www.test.com">
<head>
<title>Test</title>
<script type="text/javascript">
// We want to find all the question tags withing a page tag
// getElementsByTagName works fine when using standard HTML tags within each other
function BuildTree() {
var total = 0;
// These two do not work in IE but do work in FF
//var pages = document.getElementsByTagName('custom:page');
//var questions = pages[0].getElementsByTagName('custom:question');
// These only work in FireFox (and require a namespace)
//var pages = document.getElementsByTagNameNS('http://www.test.com', 'page'); // FF only
//var questions = pages[0].getElementsByTagNameNS('http://www.test.com', 'question'); // FF only
// Adding a namespace ' xmlns:custom="http://www.test.com"' to the document and removing the 'custom:' from the search works correctly
var pages = document.getElementsByTagName('page');
var questions = pages[0].getElementsByTagName('question');
for (var i = 0; i < questions.length; ++i) {
var question = questions[i];
var val = question.getAttribute('value')
total += val;
}
alert("Total: " + total);
};
</script>
</head>
<body>
<custom:pages>
<custom:page>
<custom:question value="1">aaa</custom:question>
<custom:question value="2">bbb</custom:question>
<custom:question value="3">ccc</custom:question>
</custom:page>
<custom:page>
<custom:question value="1">aaa</custom:question>
<custom:question value="2">bbb</custom:question>
<custom:question value="3">ccc</custom:question>
</custom:page>
</custom:pages>
<input id="btnTest" type="button" value="Test" onclick="BuildTree();" />
</body>
</html>
为了使它工作,你需要制作页面XHTML并定义你的自定义命名空间,然后使用getElementsByTagNameNS。也可以使用 XSLT。
相关文章:
- Highcharts-如何在自定义格式化程序中获得默认的y轴标签格式化程序
- 不使用自定义CSS或HTML(使用框架方法)的角度材质文本输入或文本区域标签大小
- 在javascript中调用自定义谷歌搜索标签时出现问题
- 如何为javascript创建自定义标签?或者这是我不知道的其他事情吗;我不知道
- 圆环饼图中数据标签的自定义CSS类
- 动态自定义文本堆栈标签Highcharts
- 我使用谷歌标签管理器在我的网站上启动标签.变量“;未定义的“;在任何版本的Internet Explorer的自定义代码
- 如果JavaScript标签是通过谷歌标签管理器插入到页面上的,那么它们的延迟程度是多少;s的自定义HTML
- 如何在没有代码隐藏访问权限的情况下将自定义 HTML 属性添加到 ASP.NET 标签
- 将图表工具提示自定义为标签,其中线条指向饼图段
- 需要帮助从数组制作折线图,我想要自定义刻度标签
- JQPLOT 根据标签名称为饼图设置自定义颜色
- Google 地图使用自定义标签对地址进行地理编码
- 高图表自定义 xAxis 标签
- 允许 HTML 的角度树视图中的自定义标签
- 是否可以使用自定义标签代替
标签
- 自定义 Joomla 3.x 标签页面
- 我可以在 RichFace 中为文件上传实现多个文件选择,或者使用 javascript 为此实现自定义标签吗?
- D3.js如何在水平对数缩放轴上放置自定义标签
- 使用 Javascript 更改 Google 自定义搜索中的标签页