2
<table id="droppable" border="1" style="position: relative">
    <tr> 
      <td>a1</td>
      <td>b1</td>
      <td>c1</td>
      <td>d1</td>
      <td>e1</td>
      <td>f1</td>
    </tr>
    <tr> 
      <td>a2</td>
      <td>b2</td>
      <td>c2</td>
      <td>d2</td>
      <td>e2</td>
      <td>f2</td>
    </tr>
    <tr>
     <td>a3</td>
     <td>b3</td>
     <td>c3</td>
     <td>d3
     <div id="abs" style="position: absolute; height: 50px; width: 50px;background-   color: red">asdasd</div>
    </td>
    <td>e3</td>
    <td>f3</td>
    </tr>
    <tr> 
     <td>a4</td>
     <td>b4</td>
     <td>c4</td>
     <td>d4</td>
     <td>e4</td>
     <td>f4</td>
    </tr>
    <tr> 
    <td>a5</td>
    <td>b5</td>
    <td>c5</td>
    <td>d5</td>
    <td>e5</td>
    <td>f5</td>
    </tr>
    <tr> 
     <td>a6</td>
     <td>b6</td>
     <td>c6</td>
     <td>d6</td>
     <td>e6</td>
     <td>f6</td>
   </tr>
</table>

​​直播:http : //jsfiddle.net/qYNjP/1/

如何使用 jQuery 获取 div#abs 下的所有元素?

在这个例子中,我想接收带有 td {d4, d5, d6, e4, e5, e6, f4, f5, f6} 的对象

4

5 回答 5

3

one way to get the elements is to check the coords and dimensions of the cells.

use this 2 functions:

function getRectangle (obj) {

   var off = obj.offset();

   return {
          top: off.top,
          left: off.left,
          height: obj.outerHeight(),
          width: obj.outerWidth()
   };
}

function inCoords (x, y, rect) {

        if ((x > rect.left && x < (rect.left + rect.width))
            && (y > rect.top && y < (rect.top + rect.height)))
            return true;

        return false;
}

with getRectangle you should first save the data for your div. In the next step you go through all td's document.getElementsByTagName("td"); ... and check with "inCoords" if the left and top of the cell is in the rectangle of the div. maybe you calc the bottom right corner with left + width and top + height so you can check if this corner is under your div.

i hope this helps!

greetings

于 2012-11-08T11:55:44.737 回答
3

您可以为特定的 td 上课,而不是使用 Id

于 2012-11-08T11:54:35.087 回答
0
$('#abs').siblings()

兄弟姐妹():获取匹配元素集中每个元素的兄弟姐妹,可选地由选择器过滤。

在此处查看完整文档。

于 2013-01-02T06:01:30.323 回答
0

利用

 $('#abs').siblings()

.siblings( [selector] ) 获取匹配元素集中每个元素的兄弟姐妹,可选地由选择器过滤。

<ul>
<li>list item 1</li>
<li>list item 2</li>
<li class="third-item">list item 3</li>
<li>list item 4</li>
<li>list item 5</li>
</ul>
$('li.third-item').siblings().css('background-color', 'red');

检查这个

或者你可以给类名

<td class="position"> d4 </td>
<td class="position"> d5 </td>
<td class="position"> d6 </td>
<td class="position"> e4 </td>
<td class="position"> e5 </td>
<td class="position"> e6 </td>
<td class="position"> f4 </td>
<td class="position"> f5 </td>
<td class="position"> f6 </td>

$('.position').css('background-color', 'red');
于 2012-11-08T12:20:05.943 回答
-1

你可以试试这样的东西吗?

小提琴

PS。只引用实际上属于问题的标签

<table id="droppable" border="1" style="float: left; clear: both">
    <tr>
        <div id="abs" style="float:left; height: 50px; width: 50px;background-color: red">asdasd</div><td>a1</td><td>b1</td><td>c1</td><td>d1</td><td>e1</td><td>f1</td></tr>
    <tr> <td>a2</td><td>b2</td><td>c2</td><td>d2</td><td>e2</td><td>f2</td></tr>
    <tr> <td>a3</td><td>b3</td><td>c3</td><td>d3</td><td>e3</td><td>f3</td></tr>
    <tr> <td>a4</td><td>b4</td><td>c4</td><td>d4</td><td>e4</td><td>f4</td></tr>
    <tr> <td>a5</td><td>b5</td><td>c5</td><td>d5</td><td>e5</td><td>f5</td></tr>
    <tr> <td>a6</td><td>b6</td><td>c6</td><td>d6</td><td>e6</td><td>f6</td></tr>
</table>
于 2012-11-08T11:52:47.617 回答