Opera/Firefox Javascript 引擎差异

发布于 2024-11-08 02:53:45 字数 526 浏览 0 评论 0原文

我想知道是否有其他人经历过使用 javascript 的浏览器之间的这种“故障”。

我的 javascript 如下,

  var theForm = document.getElementById( 'theForm' );

  theForm.firstname = theForm.firstName.value.trim();
  theForm.lastname = theForm.lastName.value.trim();


  theForm.firstName.style.color = "red";

这似乎在 Opera 11 中不起作用,但在 Firefox 4 中起作用。

我只是认为这两个 javascript 引擎处理事情的方式不同。

当我在两者中调试 javascript 时,我得到不同的结果。

在 Opera 中,theForm.firstName 在赋值后会变成常规的旧字符串,但在 Firefox 中它仍然是一个表单元素。

还有其他人经历过吗?

I wanted to know if anyone else has experienced this 'glitch' between browsers with javascript.

My javascript was the following

  var theForm = document.getElementById( 'theForm' );

  theForm.firstname = theForm.firstName.value.trim();
  theForm.lastname = theForm.lastName.value.trim();


  theForm.firstName.style.color = "red";

This doesn't seem to be working in Opera 11 but it works in Firefox 4.

I just think that the two javascript engines are handling things differently.

When I debug the javascript in both I get different results to.

In Opera, theForm.firstName turns into a regular old string after assignment but in Firefox it stays a form element.

Has anyone else experienced this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

如梦 2024-11-15 02:53:45
<form id="bar">
  <input name="foo">
</form>

var form = document.getElementById("bar");

form.foo; // is a DOM element

form.foo = form.foo.value.trim(); // trying to set a dom element to a string??

form.foo; // What am I?

如果您隐藏了 form["someName"] ,浏览器如何知道它是表单内的属性还是 DOM 元素。

垃圾进来,垃圾出去。

<form id="bar">
  <input name="foo">
</form>

var form = document.getElementById("bar");

form.foo; // is a DOM element

form.foo = form.foo.value.trim(); // trying to set a dom element to a string??

form.foo; // What am I?

How is a browser supposed to know whether form["someName"] is a property or a DOM element inside the form if you shadow it.

Garbage in, Garbage out.

作死小能手 2024-11-15 02:53:45

就像 Raynos 所说:你正在将 DOM 元素设置为字符串。将您的代码更改为

var theForm = document.getElementById( 'theForm' );
theForm.firstname.value = theForm.firstName.value.trim();
theForm.lastname.value = theForm.lastName.value.trim();
theForm.firstName.style.color = "red";

Like Raynos said: You're setting a DOM element to a string. Change your code to be

var theForm = document.getElementById( 'theForm' );
theForm.firstname.value = theForm.firstName.value.trim();
theForm.lastname.value = theForm.lastName.value.trim();
theForm.firstName.style.color = "red";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文