简体   繁体   English

Javascript在Mozilla Firefox中不起作用

[英]Javascript doesn't work in Mozilla Firefox

hi everyone i write the following code 大家好,我写下面的代码

<form name=f>
<input type=button value="Button1" onclick=b1click()>
<input type=button value="Buttone2" onclick=b2click()>
<script language=javascript>
function b1click()
{
  f.action="Login.jsp";
  f.submit();
}
function b2click()
{
  f.action="Logout.jsp";
  f.submit();
}
</script>
</form>

This code properly work in Internet Explorer but The action does not work in Mozilla Firefox 3.6.2 how to solve this problem? 此代码可在Internet Explorer中正常工作,但该操作在Mozilla Firefox 3.6.2中不起作用如何解决此问题? Please any one help me. 请任何人帮助我。 Thanks 谢谢

I know this will sound snide, but the truth of the matter is: it's not 1995 anymore . 我知道这听起来很愚蠢,但这件事的真相是: 不再1995年了

That code would have worked great a decade ago, but standards and specifications have changed significantly since then. 该代码在十年前就可以很好地工作,但是自那时以来,标准和规范已发生了巨大变化。

Lets start from the top: 让我们从顶部开始:

<form name=f>

All html attribute values should be enclosed in quotes. 所有 html属性值都应用引号引起来。 For consistency sake, use double quotes: <form name="f"> is much better. 为了保持一致性,请使用双引号: <form name="f">更好。

<input type="button" value="Button1" onclick="b1click()">

Avoid inline-script events. 避免内联脚本事件。 If the functionality ever changes, or you want to remove a function, you'll have to go through every page and adjust the function. 如果功能发生了变化,或者您想删除某个功能,则必须浏览每个页面并调整该功能。 A better way is to give the button an ID, and add the onclick event via scripts: 更好的方法是给按钮一个ID,然后通过脚本添加onclick事件:

HTML:
<input type="button" value="Button1" id="button1">
JS:
document.getElementById('button1').onclick = b1click;

Now the script's turn: 现在该脚本了:

<script language=javascript>

You should use the type attribute with a valid MIME type . 您应该将type属性与有效的MIME类型一起使用 Additionally, whenever possible, move your scripts to an external script file. 此外,只要有可能,请将您的脚本移至外部脚本文件。 When that's not possible, make sure to either XML encode your script, or encase it in CDATA tags: 如果无法做到这一点,请确保对脚本进行XML编码,或将其包含在CDATA标记中:

<script type="text/javascript" src="path/to/script.js"></script>

OR 要么

<script type="text/javascript">
/* <![CDATA[ */
... some code ...
/* ]]> */
</script>

Finally the real issue with your script. 最后是脚本的真正问题。

The f property you're referencing is a member of the document , and not the window . 您所引用的f属性是document的成员,而不是 window I believe IE will put the reference on both, but it's just not safe to rely on either behavior. 我相信IE会同时引用这两种方法,但是依靠任何一种行为都是不安全的。

Give the form an ID: <form id="f"> , and get the element from the b[12]click functions 给该表单一个ID: <form id="f"> ,然后从b[12]click函数中获取元素

function b1click()
{
  var f = document.getElementById('f');
  f.action = 'Login.jsp';
  f.submit();
}

First off, change that name="foo" to id="foo" . 首先,将name="foo"更改为id="foo" Names are mostly used within the form itself. 名称通常表单本身中使用。

Now, try to reference your form using document.formID , not just formID . 现在,尝试使用document.formID而不是formID来引用表单。 formID is a variable, which is undefined, but document.formID is the actual form element: formID是一个未定义的变量,但是document.formID是实际的表单元素:

function b1click()
{
  document.f.action="Login.jsp";
  document.f.submit();
}
function b2click()
{
  document.f.action="Logout.jsp";
  document.f.submit();
}

给表单一个ID,并使用以下内容引用它:

var form = document.getElementById('formId');

You should quote the input attributes, or any attributes for that matter. 您应该引用输入属性或与此相关的任何属性。 And your script does not belong AFTER the form, eg in body , but rather in the HEAD element. 并且您的script不属于表单之后,例如,在body ,而是在HEAD元素中。

This works in IE, Firefox and Chrome. 这适用于IE,Firefox和Chrome。

<html>
<head>
<script language="javascript">
function b1click()
{
  f.action="Login.jsp";  // better is document.f., but f. appears to work as well
  f.submit();
}
function b2click()
{
  f.action="Logout.jsp";
  f.submit();
}
</script>
</head>
<body>
<form name="f">
<input type="button" value="Button1" onclick="b1click()">
<input type="button" value="Buttone2" onclick="b2click()">
</form>
</body>
</html>

There are a couple ways to reference your form. 有两种方法可以引用您的表单。

If you define your form as <form name="Login" id="LoginFrom"></form> , 如果您将表单定义为<form name="Login" id="LoginFrom"></form>

Method 1 方法1

If your form is the only one in the page, you can use: 如果您的表单是页面中唯一的表单,则可以使用:

document.forms[0].action = 'Login.jsp';

Method 2 方法二

If your form is not the only one form in the page, you can use the form name to reference the form, such as 如果您的表单不是页面中唯一的表单,则可以使用表单名称来引用该表单,例如

document.Login.action = 'Login.asp';

Method 3 方法3

The form can also be referenced with DOM function getElementByID . 表单也可以使用DOM函数getElementByID引用。

document.getElementByID('LoginForm').action = 'Login.asp'

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

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