why this jQuery does not work?
I have this table with some styled tds (I put screenshot because the content of td is pretty big)
Now, I want to add some css to this table so I try to select its 开发者_开发知识库td by one if its classes and apply the style like:
jQuery(".graTabbedComponentDefaultTabs").parent('table').css('position','relative');
But nothing happens....Do you see the problem?
Thanks in advance.
You want to use closest()
instead of parent()
.
jQuery(".graTabbedComponentDefaultTabs").closest('table').css('position','relative');
parent()
grabs the direct parent of an element (if it matches the selector). closest()
traverses up the DOM until it finds the selector you want and then stops. parents()
traverses up the DOM and grabs all elements that match the selector.
You need to use parents(), not parent().
Parent() will look 1 level up in the DOM. Parents() will traverse up the DOM until it finds a matching element.
jQuery(".graTabbedComponentDefaultTabs").parents('table').css('position','relative');
<table>
is not the parent of your <td>
- use .parents('table')
instead to get the ancestor instead of the immediate parent.
Use parents instead of parent:
jQuery(".graTabbedComponentDefaultTabs").parents('table').css('position','relative');
Because the parent of elements with that class are <tr>
s and not a <table>
, your parent('table')
call will return nothing.
Use Closest. NOT parents, as this may select a table that is the parent of that table.
$(".graTabbedComponentDefaultTabs")
.closest('table')
.css('position','relative');
精彩评论