Javascript-firefox:表格行无法突出显示

发布于 2024-12-15 04:43:08 字数 702 浏览 7 评论 0原文

document.onmouseover= function(event) {
    if (event===undefined) event= window.event;                     
    var target= 'target' in event? event.target : event.srcElement; 
    if(target.tagName == 'TR') {            
            target.style.backgroundColor = 'red';
        }else{
            target.style.backgroundColor = "yellow";
        }
};
document.onmouseout= function(event) {
    if (event===undefined) event= window.event;                     
    var target= 'target' in event? event.target : event.srcElement; 
    target.style.backgroundColor = "transparent";
};

TR 元素从不突出显示,也不会为此触发 onmouseover 事件。 TD元件工作正常。什么解释了这种行为以及如何让 onmouseover 在触及 TR 元素时触发?

document.onmouseover= function(event) {
    if (event===undefined) event= window.event;                     
    var target= 'target' in event? event.target : event.srcElement; 
    if(target.tagName == 'TR') {            
            target.style.backgroundColor = 'red';
        }else{
            target.style.backgroundColor = "yellow";
        }
};
document.onmouseout= function(event) {
    if (event===undefined) event= window.event;                     
    var target= 'target' in event? event.target : event.srcElement; 
    target.style.backgroundColor = "transparent";
};

TR elements never highlight nor does onmouseover event fire for this. TD elements work fine. What explains this behavior and how can I make onmouseover fire when it touches TR element?

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

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

发布评论

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

评论(4

风柔一江水 2024-12-22 04:43:08

如何将代码更改为:

var rows = document.getElementsByTagName('TR');
for (var i = 0, l = rows.length; i < l; i++) {
  rows[i].onmouseover = function (event) {
    this.style.backgroundColor = 'red';
  }

  rows[i].onmouseout = function (event) {
    this.style.backgroundColor = "transparent";
  }
}

演示: http://jsfiddle.net/Asv4v/3/

How about changing your code to:

var rows = document.getElementsByTagName('TR');
for (var i = 0, l = rows.length; i < l; i++) {
  rows[i].onmouseover = function (event) {
    this.style.backgroundColor = 'red';
  }

  rows[i].onmouseout = function (event) {
    this.style.backgroundColor = "transparent";
  }
}

Demo: http://jsfiddle.net/Asv4v/3/

软糖 2024-12-22 04:43:08

我的猜测是,这与冒泡/捕获模型有关。在文档本身上声明处理程序会在事件到达文档之前留下许多可以捕获事件的元素。不管怎样,我做了类似的事情,但是将我的处理程序放在 元素上。还值得注意的是,大多数主要的 javascript 库都有框架来帮助解决这个问题。我个人喜欢 YUI 或 Prototype,尽管许多其他人更喜欢 Dojo 或 JQuery。

My guess would be that this would have something to do with the bubbling/capturing model. Declaring the handler on the document itself leaves a lot of elements in between that could be capturing the event before it reaches document. Anyway, I do something similar to this, but put my handler on the <table> element. It is also worth noting that most major javascript libraries have frameworks to help with this. I personally like YUI or Prototype, though many other prefer Dojo or JQuery.

美胚控场 2024-12-22 04:43:08

您可以使用:

document.onmouseover = function(e){
    if (e.target.tagName.toLowerCase() == 'td'){
        e.target.parentNode.style.backgroundColor = 'red';
        e.target.onmouseout = function(){
            this.parentNode.style.backgroundColor = 'white';
        }
    } 
};

JS Fiddle 演示

You could use:

document.onmouseover = function(e){
    if (e.target.tagName.toLowerCase() == 'td'){
        e.target.parentNode.style.backgroundColor = 'red';
        e.target.onmouseout = function(){
            this.parentNode.style.backgroundColor = 'white';
        }
    } 
};

JS Fiddle demo.

夏了南城 2024-12-22 04:43:08

你的问题是你的 tr 完全包含它的 tds,因此 TD 与 DOM 中的 TR 重叠;即,“目标”永远不会是TR。要测试这一点,请制作 2 个 TR,第一个具有 2 个 TD,第二个具有 1 个(或查看链接的 jsfiddle)

http ://jsfiddle.net/Asv4v/6/

编辑:有趣的是,在 FFox 中,如果缺少 TD,则目标是“TR”,但在 chrome 中,目标是“Table”

简短回答:为了获得我认为您正在寻找的行为,请将 js 中的“TR”替换为“TD”

Your problem is that your tr exactly contains it's tds, and hence the TDs overlap the TRs in the DOM; i.e., the "target" will never be the TR. To test this, make 2 TRs, the first with 2 TDs and the second with 1 (or see the linked jsfiddle)

http://jsfiddle.net/Asv4v/6/

Edit: Interestingly enough, in FFox if there is a missing TD the target is a "TR", but in chrome the target is a "Table"

Short answer: To get the behaviour I think you're looking for replace the "TR" in the js to "TD"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文