Opera / Firefox Javascript引擎的差异

时间:2011-05-18 17:59:28

标签: javascript firefox opera

我想知道是否有其他人在使用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中它仍然是表单元素。

还有其他人经历过这个吗?

2 个答案:

答案 0 :(得分:2)

<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元素。

Garbage in,Garbage out。

答案 1 :(得分:1)

像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";
相关问题