简体   繁体   English

Javascript Web浏览器脚本不起作用

[英]Javascript web browser script not working

Internet Explorer 7 does not handle buttons correctly since it sends the text between <button> and </button> rather than the value. Internet Explorer 7无法正确处理按钮,因为它在<button></button>之间发送文本而不是值。 (Weird but true.) This Javascript is supposed to correct this by sending the value of the button onClick . (很奇怪,但确实如此。)该Javascript应该通过发送按钮onClick的值来纠正此问题。 But it does not work. 但这行不通。 Can anybody see the error ? 有人可以看到错误吗?

I use this to attach the Javascript file in HTML5 : 我用它来附加HTML5中的Javascript文件:

<script src="buttonfix.js"></script>

Contents of buttonfix.js : buttonfix.js的内容:

function runonclick()
{
    var count = 0;
    var formlength = this.form.elements.length;
    while(count < formlength)
    {
        if(this.form.elements[count].tagName === "button")
        {
            this.value = this.attributes.getNamedItem("value").nodeValue;
        }

        count++;
    }
}

function buttonfix()
{
    var buttonarray = document.getElementsByTagName("button");

    var count = 0;
    var buttonarraylength = buttonarray.length;
    while(count < buttonarraylength)
    {
        buttonarray[count].onClick = runonclick();

        count++;
    }
}

window.attachEvent("onload", buttonfix());

I suggest you to try: 我建议您尝试:

if(this.form.elements[count].tagName.toLowerCase == "button");

instead of: 代替:

if(this.form.elements[count].tagName == "button")

because the tagName property generally returns an uppercased string. 因为tagName属性通常返回大写的字符串。

Another line does not work: 另一行不起作用:

buttonarray[count].onClick = runonclick();

As JavaScript it is a case-sensitive language, you have to use the standard name of the event ( onclick ). 由于JavaScript是区分大小写的语言,因此您必须使用事件的标准名称( onclick )。

buttonarray[count].onclick = runonclick();

However, you may still to use onClick in your HTML (but not XHTML). 但是,您仍然可以在HTML(而不是XHTML)中使用onClick

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

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