简体   繁体   English

单击表格单元格内的链接时显示div

[英]Show a div when clicking on a link inside a table cell

Probably very simple thing, but I can't figure it out. 可能很简单,但我无法弄清楚。 I want to toggle the visibility of a div, when clicking on a link. 我想在点击链接时切换div的可见性。

So this is my situation 所以这是我的情况

<table width="100%">
   <tr>
      <td>
         <a onclick='showDiv' />
      </td>
   </tr>
</table>
<div name='divToShow' style='display:none;' />

The code I had so far: 我到目前为止的代码:

// Then show the panel with substitute information
var control = $(parent).parent().parent().parent().find('div[name$="divToShow"]');
control.toggle();

So I'm not sure how to achieve this. 所以我不确定如何实现这一目标。 Anyone? 任何人?

Thanks, Daniel 谢谢,丹尼尔

Why not assigning an ID to the div and then show it using $('#id').toggle()? 为什么不给div分配一个ID,然后使用$('#id')。toggle()来显示它?

Basic example: http://jsfiddle.net/fVXAC/ 基本示例: http//jsfiddle.net/fVXAC/

You may use jQuery's toggle() function. 您可以使用jQuery的toggle()函数。

something like; 就像是;

$('#clickme').toggle(
function() {
    $('#mydiv').css({ 'display':'block' });
}, function() {
    $('#mydiv').css({ 'display':'none' });
});

Here is the working demo: http://jsfiddle.net/CXHAW/ 这是工作演示: http//jsfiddle.net/CXHAW/

Try this 尝试这个

http://jsfiddle.net/Eez4g/1/ http://jsfiddle.net/Eez4g/1/

I think that's what you are trying to do 我想这就是你要做的

Elements like <a> and <div> needs to be closed with </a> and </div> , and are not self closing. <a><div>这样的元素需要用</a></div>关闭,而不是自动关闭。

An <a> element should also have a href attribute, even if it's just href="#" . <a>元素也应该具有href属性,即使它只是href="#"

$("a[onclick='showDiv']")​​​.on('click', function(e) { //find element based on onclick, would be better with an ID
    e.preventDefault(); //all <a> elements should have a href
    $(this).closest('table').next($("[name='divToShow']")).toggle();
})​​​​;​

FIDDLE 小提琴

If you start by understanding HTML and writing the correct markup, using javascript will become a lot easier for you. 如果您从了解HTML并编写正确的标记开始,使用javascript将变得更加容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM