简体   繁体   English

div 是否出现在带有 jquery/javascript 的 TD 的 TD onclick 的右侧?

[英]Have div appear to the right of a TD onclick of TD with jquery/javascript?

So I have a absolute positioned div of 5x5 pixel square.所以我有一个绝对定位的 5x5 像素正方形的 div。

If I have a table say如果我有一张桌子说

<table><tr><td>Something</td></tr></table>

I want the div to show up exactly to the right of the TD when I click anywhere inside of the TD, so it should appear snapped to the right side of the TD regardless of where I click in the TD.当我单击 TD 内的任何位置时,我希望 div 恰好显示在 TD 的右侧,因此无论我在 TD 中的哪个位置单击,它都应该显示在 TD 的右侧。 Is there a simple way to accomplish this in Jquery/Javascript?有没有一种简单的方法可以在 Jquery/Javascript 中实现这一点?

I'd suggest using hidden table cells, and showing the div within that:我建议使用隐藏的table单元格,并在其中显示div

<table>
    <tr>
        <td>Something</td>
        <td class="hidden"><div></div></td>
    </tr>
    <tr>
        <td>Something</td>
        <td class="hidden"><div></div></td>
    </tr>
</table>

jQuery: jQuery:

$('tr td').click(
    function(){
        $(this).closest('tr').find('.hidden').toggle();
    });

CSS: CSS:

td {
    vertical-align: middle;
    width: 5em;
    height: 2em;
}

.hidden {
    display: none;
}

.hidden div {
    display: block;
    height: 5px;
    width: 5px;
    background-color: #f90;
}

JS Fiddle . JS小提琴


Edited to revise the content a little, so that the containing ( .hidden ) td is always visible (to reduce the chance of page jumping around due to suddenly appearing table cells): 编辑对内容进行了一些修改,使包含的 ( .hidden ) td始终可见(以减少由于突然出现的表格单元格而导致页面跳转的机会):

jQuery: jQuery:

 $('tr td').click( function() { $(this).closest('tr').find('.hidden div').toggle(); });

CSS: CSS:

 table { empty-cells: show; } td { vertical-align: middle; width: 5em; height: 2em; } td:hover { background-color: rgba(255,255,0,0.2); }.hidden div { display: none; }.hidden div { height: 5px; width: 5px; background-color: #f90; }

JS Fiddle . JS小提琴


Edited as per @Nicky Waite's suggestion (below): 根据@Nicky Waite 的建议进行编辑(如下):

Maybe stick your hover on the tr element.也许将您的 hover 贴在 tr 元素上。

$('tr').click(

function() {
    $(this).find('.hidden div').toggle();
});

JS Fiddle demo . JS 小提琴演示

give the TD relative position.给TD相对position。 and put the div inside it.并将 div 放入其中。 give your div给你的 div

right:0px;

on click点击

give your div style of:给你的 div 风格:

left:50%;
margin-left:-2px //(these might need to be change a bit)

Select the <td> that you want to put a <td> after using a jQuery selector (either using a class or id), and use $.append() Select 在使用 jQuery 选择器(使用 class 或 id)之后要放置<td> <td> <td>,并使用 $.append()

Here are your options for selecting a jQuery tag以下是您选择 jQuery 标签的选项

http://api.jquery.com/category/selectors/ http://api.jquery.com/category/selectors/

So something like所以像

$("td").click(function(event) {
     $(this).append("<td></td>");
});

Yes, you could handle it in a few ways.是的,您可以通过几种方式处理它。 Here's one thought:这里有一个想法:

$("td").click(function(e) {
  el = $(e.target);
  el.after("<div class='my_square' />");
  sq = $(el + " + .my_square");
  sq.css({
    position: "absolute",
    left: el.offset().left,
    top: el.offset().top + el.outerHeight() / 2
  })
})

Here is another solution using positions.这是使用职位的另一种解决方案。 Although I wasn't sure if you wanted multiple boxes or just one.虽然我不确定你是想要多个盒子还是只想要一个。

http://jsfiddle.net/nickywaites/DvTBR/ http://jsfiddle.net/nickywaites/DvTBR/

$(function() {

$('#summary td').click(function() {

    var td = $(this);
    var position = td.position();
    $('#link').css({
        top: position.top + (td.height() / 2),
        left: position.left + td.width() + 10
    }).show();

});

});

<div id="link"></div>
<table id="summary">
<tr><td>Toast</td></tr>
<tr><td>Toast</td></tr>
</table>

#link {
width:5px;
height:5px;
background-color:royalblue;
display:none;  
position:absolute;
}

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

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