简体   繁体   English

getElementsByTagName()在IE和FF中的行为有所不同

[英]getElementsByTagName() behaves differently in IE and FF

I am new to JavaScript, trying to figure out a tag information value. 我刚接触JavaScript,试图找出标签信息值。 GWT Code is as follows.. GWT代码如下。

public static native boolean isToolBarInstalled() /*-{
    alert("Validating the toolbar installed.");
    var metas = document.getElementsByTagName('head')[0].getElementsByTagName('meta');
        var i;
        alert ("Meta length: "+metas.length);
        for (i = 0; i < metas.length; i++){
            alert("Value: "+metas[i].value);
            if (metas[i].getAttribute('name') == "toolbar"){
                return true;
            }
        }

        return false;
        }-*/;

FF return true whereas IE returns false, for the same page? FF对于同一页返回true,而IE返回false? Any clue/suggestions would be helpful. 任何线索/建议都将有所帮助。

WM. WM。

HTML is too huge to post, here is a snippet of the code.. HTML太大而无法发布,下面是代码片段。

<html>
<head>
    ....
    <title>My App</title>
    <meta name="toolbar" content="1.0">
</head>
<body>
.....
</body>
<html>

Try this: 尝试这个:

element.attributes[value].nodeAttribute;

As explained here : 如此处所述

The getAttribute() method will return "null" in Internet Explorer when trying to get the for attribute of a label element. 尝试获取标签元素的for属性时,getAttribute()方法在Internet Explorer中将返回“ null”。

Might be the same issue... 可能是同一个问题...

Well look: 好看:

In this example it is working on IE. 在此示例中,它正在IE上运行。

function isToolBarInstalled() {
    alert("Validating the toolbar installed.");
    var metas = document.getElementsByTagName('head')[0].getElementsByTagName('meta');
    var i;
    alert ("Meta length: "+metas.length);
    for (i = 0; i < metas.length; i++){
        alert("Value: "+metas[i].value);

        var attr = metas[i].getAttribute('name');

        // IE workaround
        if (attr == null)
        {
            attr = metas[i].attributes["name"];
            if (attr != null) attr = attr.nodeValue;
        }

        if (attr == "toolbar")
            return true;
    }

    return false;
}

alert( "Is installed: " + isToolBarInstalled() );

Works for me in IE7. 在IE7中为我工作。

Check you haven't got any text content before the <meta> end-tag other than simple whitespace. 检查您是否在<meta>结束标记之前没有任何文本内容,除了简单的空白。

If you have any non-whitespace text in or before <html> or <head> , the browser will decide that you meant to open the <body> to contain the text. 如果在<html><head>之前或之前有任何非空白文本,浏览器将决定您要打开<body>来包含该文本。 (This is actually valid in non-XHTML HTML, as the </head> end-tag and the <body> start-tag are optional.) That means closing the <head> section, so the number of <meta> tags inside <head> will be 0. (这在非XHTML HTML中实际上是有效的,因为</head>结束标记和<body>起始标记是可选的。)这意味着关闭<head>部分,因此内部的<meta>标记数目<head>将为0。

In any case you might as well say just: 无论如何,您最好只说:

var metas= document.getElementsByTagName('meta');

as the bit about checking they're in <head> is redundant for a valid document; 因为检查它们是否在<head>中对于有效文档是多余的; that's the only place <meta> is allowed to appear. 这是允许<meta>出现的唯一位置。

        alert("Value: "+metas[i].value);

There's no .value on <meta> , do you mean .content ? <meta>上没有.value ,您是说.content吗?

        if (metas[i].getAttribute('name') == "toolbar"){

Use metas[i].name . 使用metas[i].name There's no reason to use getAttribute / setAttribute on an HTML document and there are problems with it on IE. 没有理由在HTML文档上使用getAttribute / setAttribute ,并且在IE上存在问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM