offsetParent()方法是jQuery中的一个内置方法,用于查找DOM树中第一个定位的父元素。 语法:
null
$(selector).offsetParent()
参数: 此方法不包含任何参数。 返回值: 此方法返回第一个定位的父元素。 下面的示例演示了jQuery中的offsetParent()方法: 例子:
html
<!DOCTYPE html> < html > < head > < title >The offsetParent Method</ title > < script src = </ script > <!-- jQuery code to show the working of this method --> < script > $(document).ready(function(){ $("button").click(function(){ $("p").offsetParent().css("background-color", "green"); }); }); </ script > < style > #d1 { border:1px solid black; width:70%; position:absolute; left:10px; top:50px } #d2 { border:1px solid black; margin:50px; background-color:lightblue; text-align:center; } p { padding-left: 80px; font-weight: bold; } </ style > </ head > < body > < button >Submit</ button > < div id = "d1" > < div id = "d2" > <!-- click on this paragraph --> < p >Welcome to GeeksforGeeks!</ p > </ div > </ div > </ body > </ html > |
输出:
相关文章:
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END