我想创建一个 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.
发布评论
评论(8)
如果您想使用 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!).
詹姆斯,这符合你的要求。可以将此代码放在任何带有表格的页面上以获得突出显示的列效果。我会使用这个解决方案,而不是创建我自己的自定义表格结构,因为它与现有代码兼容,它允许表格数据显示在它所属的位置,并且 jQuery 在几行代码中处理所有繁重的工作。
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.
您可以像这样使用 Javascript onclick 事件:
或
获得更多自定义功能。
我不知道您是否只想单击每列顶部的单元格,但如果您想对整个列执行此操作,您可以将此事件添加到列中的每个单元格。
You can use Javascript onclick event like so:
or
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.
例如,您有以下 html
jquery
http://jsfiddle.net/jtDP8/4/
e.g. you have the following html
jquery
http://jsfiddle.net/jtDP8/4/
您可以通过jQuery轻松地将
click
和mouseover
事件绑定到每个td
。更新:分离单元格索引和行索引并将主题保存为每个
td
的数据:在 jsfiddle 上查看完整演示
You can bind
click
andmouseover
events to eachtd
easily by jQuery.UPDATE: separate the cell-index and row-index and save theme as each
td
's data:See the full-demo here at jsfiddle
我会使用现有的 jQuery 表解决方案,但如果您只想检测单击的列,您可以这样做: http ://jsfiddle.net/rkw79/PagTJ/
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/
我认为这取决于您的目标市场。如果您可以使用 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.
(1)如果你说的是动态链接,那就是jQuery。
(2) 否则,我会全心全意支持你在span标签中锚链接的想法,并且只使用CSS。
如果可以的话,删掉 javascript。这样,您就可以减少加载时间(通常),并吸引未启用 JavaScript 的用户(或使用过时浏览器的用户)。如果您可以为 ?col=1 到 ?col=5 创建链接,那么就这样做。这并不难。
--
如果您有很多列,另一种选择是在其中也加入一些 PHP 并运行一个 for 循环来输出链接标记中的所有数字:例如:
(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: