Почему OffsetParent возвращает body, а не table?

<table>
			<div id="element">
				Lorem 
			</div>
	</table>

console.log(element.offsetParent); // body

Разве не table ближе?
  • Вопрос задан
  • 197 просмотров
Пригласить эксперта
Ответы на вопрос 3
dicem
@dicem

Свойство offsetParent содержит первый родительский элемент у которого CSS свойство position не равно static, либо body если его нет. То есть родителя относительно которого происходит позиционирование элемента.

Ну и собсна

The HTMLElement.offsetParent read-only property returns a reference to the element which is the closest (nearest in the containment hierarchy) positioned ancestor element. If there is no positioned ancestor element, the nearest ancestor td, th, table will be returned, or the body if there are no ancestor table elements either.

https://developer.mozilla.org/en-US/docs/Web/API/H...

Пример:
Ответ написан
Комментировать
Seasle
@Seasle Куратор тега JavaScript
А что Вы хотите получить, ближайший table? Тогда element.closest('table');
Ответ написан
Комментировать
@Che603000
c 2011 javascript
Не корректно помещать div внутрь table. Попробуйте так
<table>
   <tr> 
       <td>
          <div id="element">
              Lorem 
          </div>
       </td>
    </tr>
</table>
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы