创建具有可单击列的 HTML 表格

发布于 2024-12-12 05:10:41 字数 335 浏览 0 评论 0 原文

我想创建一个 HTML 表,您可以在其中选择列,即,当您将鼠标悬停在列上时,它们会突出显示,并且当您单击时,它们会重定向到新页面。 (例如,单击第五列将转到 column.aspx?col=5)。

问题是,HTML 表格按行工作: ...

所以我正在考虑使用浮动 来实现这一点 表示列,子 表示行,与使用 table 并实现所需效果相比使用 jQuery。

哪个会更好(以及为什么)?或者我应该考虑其他解决方案?请指教。

I'd like to create an HTML table where you can select columns, i.e. they are highlighted when you hover over them and they redirect to a new page when you click. (For example, clicking on the fifth column takes you to column.aspx?col=5).

Trouble is, HTML tables work in rows: <tr><td>...</td></tr>

So I'm deliberating between achieving this with floated <a>s to represent the columns and child <span>s to represent the rows, vs. using a table and achieving the desired effects with jQuery.

Which would be better (and why)? Or is there another solution I should consider? Please advise.

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

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

发布评论

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

评论(8

合久必婚 2024-12-19 05:10:42

如果您想使用 jQuery 执行此操作,则可以使用 -selector/" rel="nofollow">:nth-child()-Selector (假设您没有 跨越多于一列)。

有关仅定位表格第二列的示例,请参阅此小提琴

Javascript 方法的缺点是,它需要启用 Javascript(废话!)。

If you want to do this using jQuery, you can select all <td />s in a column using the :nth-child()-Selector (assuming you don't have <td />s spanning more than one column).

See this fiddle for an example of targeting onyl the 2nd column of a table.

The downside to the Javascript approach is that, well, it requires Javascript to be enabled (duh!).

女皇必胜 2024-12-19 05:10:42

詹姆斯,这符合你的要求。可以将此代码放在任何带有表格的页面上以获得突出显示的列效果。我会使用这个解决方案,而不是创建我自己的自定义表格结构,因为它与现有代码兼容,它允许表格数据显示在它所属的位置,并且 jQuery 在几行代码中处理所有繁重的工作。

<style type="text/css">
    .columnhover {background-color: yellow;}
</style>

<script type="text/javascript">
 /* <![CDATA[ */                                
    $(document).ready(function($)
    {
        $('tr th,td').hover(function()
        {
            var columnNum = $(this).parent('tr').children().index($(this));
            var $wholeColumn =  $(this).parents('table').find('td:nth-child(' + (columnNum + 1) + ')');
            $wholeColumn.addClass('columnhover');
        },
        function()
        {
            $('table').find('td').removeClass('columnhover');
        });

        $('tr th,td').click(function()
        {
            var columnNum = $(this).parent('tr').children().index($(this));
            window.location = "test.html?column=" + (columnNum + 1);
        });
    });
/* ]]> */
</script>

James, this does what you asked for. This code can be dropped on any page with tables to get the highlighted column effect. I would use this solution as opposed to creating my own custom table-like structure because it's compatible with existing code, it allows tabular data to be displayed where it belongs, and jQuery takes care of all of the heavy lifting in a few lines of code.

<style type="text/css">
    .columnhover {background-color: yellow;}
</style>

<script type="text/javascript">
 /* <![CDATA[ */                                
    $(document).ready(function($)
    {
        $('tr th,td').hover(function()
        {
            var columnNum = $(this).parent('tr').children().index($(this));
            var $wholeColumn =  $(this).parents('table').find('td:nth-child(' + (columnNum + 1) + ')');
            $wholeColumn.addClass('columnhover');
        },
        function()
        {
            $('table').find('td').removeClass('columnhover');
        });

        $('tr th,td').click(function()
        {
            var columnNum = $(this).parent('tr').children().index($(this));
            window.location = "test.html?column=" + (columnNum + 1);
        });
    });
/* ]]> */
</script>
永言不败 2024-12-19 05:10:42

您可以像这样使用 Javascript onclick 事件:

<td onclick="window.location='column.aspd?col=5'">...</td>

<td onclick="myFunction(this)">...</td>

获得更多自定义功能。

我不知道您是否只想单击每列顶部的单元格,但如果您想对整个列执行此操作,您可以将此事件添加到列中的每个单元格。

You can use Javascript onclick event like so:

<td onclick="window.location='column.aspd?col=5'">...</td>

or

<td onclick="myFunction(this)">...</td>

for more custom functionality.

I don't know if you just want to click the cell at the top of each column, but if you want to do it for a whole column you can add just this event to each cell in the column.

无远思近则忧 2024-12-19 05:10:42

例如,您有以下 html

<table>
    <thead>
        <td>1</td>
        <td>2</td>
    </thead>    
    <tbody>
        <td>google</td>
        <td>fb</td>
    </tbody>
</table>

jquery

$("thead td").click(function(){

console.log($(this).index());
    var index=$(this).index();
    console.log("column.aspx?col="+(index+1));
    //location.href="column.aspx?col="+(index+1);
});

http://jsfiddle.net/jtDP8/4/

e.g. you have the following html

<table>
    <thead>
        <td>1</td>
        <td>2</td>
    </thead>    
    <tbody>
        <td>google</td>
        <td>fb</td>
    </tbody>
</table>

jquery

$("thead td").click(function(){

console.log($(this).index());
    var index=$(this).index();
    console.log("column.aspx?col="+(index+1));
    //location.href="column.aspx?col="+(index+1);
});

http://jsfiddle.net/jtDP8/4/

Oo萌小芽oO 2024-12-19 05:10:41

您可以通过jQuery轻松地将clickmouseover事件绑定到每个td

$("table-selector tr td").each(function(){
    $(this).click(function(){
        // TODO with click
    }).hover(function(){
        // TODO with mouseover
    },function(){
        // TODO with mouseout
    });
}

更新:分离单元格索引和行索引并将主题保存为每个td的数据:

$("#myTable tr").each(function(r){
    var row = r;
    $("td", this).each(function(d){
        var cell = d;
        $(this)
            .data("rowIndex", row)
            .data("cellIndex", cell)
            .click(function(){
                    $("#message").text("Row-Index is: " + $(this).data("rowIndex") +
                                       " and Cell-Index is: " + $(this).data("cellIndex") );
                })
            .hover(
                function(){
                    $(this).addClass("td-over").css({"text-align":"center"});
                },function(){
                    $(this).removeClass("td-over").css({"text-align":"left"});
            });
    });
});

在 jsfiddle 上查看完整演示

You can bind click and mouseover events to each td easily by jQuery.

$("table-selector tr td").each(function(){
    $(this).click(function(){
        // TODO with click
    }).hover(function(){
        // TODO with mouseover
    },function(){
        // TODO with mouseout
    });
}

UPDATE: separate the cell-index and row-index and save theme as each td's data:

$("#myTable tr").each(function(r){
    var row = r;
    $("td", this).each(function(d){
        var cell = d;
        $(this)
            .data("rowIndex", row)
            .data("cellIndex", cell)
            .click(function(){
                    $("#message").text("Row-Index is: " + $(this).data("rowIndex") +
                                       " and Cell-Index is: " + $(this).data("cellIndex") );
                })
            .hover(
                function(){
                    $(this).addClass("td-over").css({"text-align":"center"});
                },function(){
                    $(this).removeClass("td-over").css({"text-align":"left"});
            });
    });
});

See the full-demo here at jsfiddle

活雷疯 2024-12-19 05:10:41

我会使用现有的 jQuery 表解决方案,但如果您只想检测单击的列,您可以这样做: http ://jsfiddle.net/rkw79/PagTJ/

$('table').delegate('td','click',function() {
    alert('column ' + $(this).index());
});

I would use an existing jQuery table solution, but if you want to just detect the column clicked, you can do this: http://jsfiddle.net/rkw79/PagTJ/

$('table').delegate('td','click',function() {
    alert('column ' + $(this).index());
});
旧街凉风 2024-12-19 05:10:41

我认为这取决于您的目标市场。如果您可以使用 JavaScript 而不必担心没有它的用​​户,那么是的,做到这一点的方法就是使用 JavaScript。

而且它将是最干净的,因为您不需要向其中添加任何类型的 HTML 噪音。你只需穿越。

I think it kind of depends on your target market. If you can use JavaScript without worrying about the users without it then yes, the way to do it is with JavaScript.

Also it's going to be the cleanest of all, since you won't be needing to add any kind of HTML noise to it. You would just traverse.

池木 2024-12-19 05:10:41

(1)如果你说的是动态链接,那就是jQuery。

(2) 否则,我会全心全意支持你在span标签中锚链接的想法,并且只使用CSS。

如果可以的话,删掉 javascript。这样,您就可以减少加载时间(通常),并吸引未启用 JavaScript 的用户(或使用过时浏览器的用户)。如果您可以为 ?col=1 到 ?col=5 创建链接,那么就这样做。这并不难。

--

如果您有很多列,另一种选择是在其中也加入一些 PHP 并运行一个 for 循环来输出链接标记中的所有数字:例如:

<?php
for($i=0;$i<200;$i++){
echo '<span><a href="?col=' . $i . '">link</a></span>';
}
?>

(1) If it is dynamic links that you are talking about, then jQuery.

(2) Otherwise, I would wholeheartedly support your idea of anchor links in span tags, and just work with CSS.

Cut the javascript if you can. This way, you're cutting down on loadtime (usually), and reaching people who don't have javascript enabled (or who have outdated browsers). If you can just create links for ?col=1 through ?col=5, then do that. It's not that difficult.

--

Another option, if you have lots of columns, is to incorporate some PHP in there too and run a for loop that outputs all of the numbers in link tags: for example:

<?php
for($i=0;$i<200;$i++){
echo '<span><a href="?col=' . $i . '">link</a></span>';
}
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文