是否在新窗口中打开表行

Open table row on new window?

本文关键字:在新窗口中打开 是否      更新时间:2023-09-26

我有一个类似于的表

<tr>
  <td><a href="http://www.example.com/">example</a></td>
  <td>another cell</td>
  <td>one more</td>
</tr>

我使用的是这个Javascript代码:

$('tr').click( function() {
    window.location = $(this).find('a').attr('href');
}).hover( function() {
    $(this).toggleClass('hover');
});

这个Javascript工作得很好,可以点击并打开一整行,但我想在选定大小的新窗口上打开表行链接。

我也有这个代码:

<td><a href="www.example.com" onclick="window.open(this.href,'targetWindow','toolbar=no,location=no,status=no,width=1000,height=600'); return false;">Example</a></td>

但它只对已定义的表单元格起作用。那个么,我如何在完整的行上创建链接,当用户点击该行时,它将以定义的大小在新窗口中打开?

1(从表行中删除所有onclick="。现在应该是这样的:

<td><a href="http://www.example.com">Example</a></td>

2( 更改jQuery中的.click(。不要忘记返回false,否则链接将同时在新窗口和当前窗口中打开。

$('tr, tr a').click( function() {
    window.open($(this).find('a').attr('href'),'targetWindow','toolbar=no,location=no,status=no,width=1000,height=600');
    return false;
})

除非我误解了你的问题,否则这将达到目的(结合你的片段(:

$('tr').click( function() {
     window.open($(this).find('a').attr('href'),'targetWindow','toolbar=no,location=no,status=no,width=1000,height=600');
}).hover( function() {
    $(this).toggleClass('hover');
});

您可以添加一个标志来区分不同的行为。

 <tr><td><a href="http://demo.com">demo</a></td></tr>
 <tr data-flag="newWindow"><td><a href="http://demo2.com">demo2</a></td></tr>

然后

$('tr').click( function(e) {
    if($(this).attr('data-flag') == "newWindow") {
        window.open($(this).find('a').attr('href'),'targetWindow','toolbar=no,location=no,status=no,width=1000,height=600');
    } else {
         window.location = $(this).find('a').attr('href');
    }
});