简体   繁体   中英

Get elements under div position:absolute with jQuery

<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>

​ ​ LIVE: http://jsfiddle.net/qYNjP/1/

How can i get all elements under div#abs with jQuery?

In this example i would like receive object with td {d4, d5, d6, e4, e5, e6, f4, f5, f6}

您可以为该特定 td 提供课程,而不是使用 Id

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"); ... 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

use

 $('#abs').siblings()

.siblings( [selector] ) Get the siblings of each element in the set of matched elements, optionally filtered by a 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');

check this

or you can give class names for

<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');
$('#abs').siblings()

siblings():Get the siblings of each element in the set of matched elements, optionally filtered by a selector.

See the full docs here .

You can try something like this?

Fiddle

PS. Only refer tags to it which actually belong to the question

<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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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