JavaScript getElementById()

标签 javascript internet-explorer getelementbyid

我正在尝试创建一个通用的 JavaScript 函数来更改事件的属性。

它的工作方式是

function fooFunction(sourceElement) 
{    
  var newName = sourceElement+'Span';    
  var newElement = document.getElementById(newName);
  //Important line
  newElement.property = "enter properties here";
}

我会用类似的东西来调用它

<img src="foo.gif" id="foo" name="foo" onmouseover="fooFunction(this.id);"/>
<span id="fooSpan" name="fooSpan">some text here</span>

所以理论上,当悬停图像时,它应该更改我需要在 fooSpan 对象上更改的任何属性。它在 Opera 中工作,但在 IE 上它返回一个 null 对象。

有什么想法吗?

我的想法是,我将拥有多个图像,它们会自动触发关联文本范围(通常是 css 样式)的属性更改。

最佳答案

您确定在 IE 中正确获取了 ID 吗?也许在 IE 中传入的 ID 为空(也许 this.id 不起作用?)。

尝试这样调用它:

<img src="foo.gif" id="foo" name="foo" onmouseover="fooFunction('foo');"/>

看看是否有帮助。我没有看到 getElementById() 失败的任何原因,所以我唯一能想到的是这是一个 ID 问题。

关于JavaScript getElementById(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/946842/

相关文章:

javascript - getElementById 无法获取 html id 的值

javascript - 创建一个使用 javascript 操作 innerHTML 的简单计算器

javascript - 如何在循环中创建一个元素并将它(一个一个地添加 5 次)到特定的 div?

javascript - 从动态变量中获取值

javascript - 为什么这个表达式在 Firefox/Chrome 中返回 true,而在 IE 中返回 false?

javascript - 带有 show(0).delay(3000).hide(0) 的 IE7 错误

javascript - IE中如何通过ID获取元素? ( Visual Studio 8?)

javascript - JavaScript 编码是否正确编写?

javascript - 模态未启动 jquery

javascript - 在 AngularJS 中为列表项加载部分 *没有* 路由