I have a table with id "tbl" and it has 2 rows and 3 cols. Each cell(td) has an explicitly defined id. Also, the table has an onclick event that calls a function foo(). The onclick() would be generated whenever any cell is clicked. The table tag is
我有一个id为“tbl”的表,它有2行3 cols。每个单元格(td)都有一个显式定义的id。无论何时单击任何单元,都会生成onclick()。表标签
< table id="tbl" width="80%" height="20%" onclick="javascript: foo()" >
< table id="tbl"宽度="80%"高="20%" onclick="javascript: foo()">
I also tried javascript:foo(this)
我也试着javascript:foo()
I want to find out the id of the table cell that was clicked.
我想查找被单击的表单元格的id。
I have tried the following javascript
我尝试过以下javascript
function foo(e)
{
var sender = (e && e.target) || (window.event && window.event.srcElement);
alert("Sender" + sender.id);
}
This works great in Google Chrome and IE, but not in Firefox. In Firefox, sender is undefined. How to get the caller cell in Firefox?
这在谷歌Chrome和IE中很有用,但在Firefox中却不行。在Firefox中,sender是未定义的。如何在Firefox中获取调用单元格?
1 个解决方案
#1
5
Firstly, remove javascript:
from the onclick
attribute. You're confusing this with javascript in the href
attribute of a link. In Javascript code, javascript:
would create a label named "javascript".
首先,从onclick属性中删除javascript:。您将此与链接的href属性中的javascript混淆。在Javascript代码中,Javascript将创建一个名为“Javascript”的标签。
Other than that, foo(event)
should work correctly with your final JavaScript code sample. The reason it doesn't work in Firefox but does in Chrome and IE is; they both support the global event object, window.event
(which is evaluated when your e.target
yields undefined
, because this
is an element which will not have a target
property). Firefox doesn't support the global event
object.
除此之外,foo(event)应该能够正确地使用最终的JavaScript代码示例。它在Firefox中不起作用,但在Chrome和IE中是;它们都支持全局事件对象窗口。事件(在您的e时进行计算。目标产生未定义的结果,因为这是一个没有目标属性的元素)。Firefox不支持全局事件对象。
Further reading:
进一步阅读:
- https://developer.mozilla.org/en/DOM:event
- https://developer.mozilla.org/en/DOM:事件
#1
5
Firstly, remove javascript:
from the onclick
attribute. You're confusing this with javascript in the href
attribute of a link. In Javascript code, javascript:
would create a label named "javascript".
首先,从onclick属性中删除javascript:。您将此与链接的href属性中的javascript混淆。在Javascript代码中,Javascript将创建一个名为“Javascript”的标签。
Other than that, foo(event)
should work correctly with your final JavaScript code sample. The reason it doesn't work in Firefox but does in Chrome and IE is; they both support the global event object, window.event
(which is evaluated when your e.target
yields undefined
, because this
is an element which will not have a target
property). Firefox doesn't support the global event
object.
除此之外,foo(event)应该能够正确地使用最终的JavaScript代码示例。它在Firefox中不起作用,但在Chrome和IE中是;它们都支持全局事件对象窗口。事件(在您的e时进行计算。目标产生未定义的结果,因为这是一个没有目标属性的元素)。Firefox不支持全局事件对象。
Further reading:
进一步阅读:
- https://developer.mozilla.org/en/DOM:event
- https://developer.mozilla.org/en/DOM:事件