700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > jquery查找父元素 子元素(个人经验总结)【jquery】

jquery查找父元素 子元素(个人经验总结)【jquery】

时间:2021-11-20 01:56:23

相关推荐

jquery查找父元素 子元素(个人经验总结)【jquery】

web前端|js教程

jquery,父元素,子元素

web前端-js教程

使用js或者jquery查找父元素、子元素经常遇到。可是用起来总容易混淆,这里统一总结了一下,以后用起来相信会方便好多

网站源码集成,ubuntu安装迅雷下载,tomcat部署工作流项目乱码,喜欢干燥炎热爬虫,PHP文件无需编译即可运行,seo 推广帖子lzw

这里jquery向上查找父元素 用到的方法:closest() parents() parent()

代挂网源码分站,vscode 网页链接,ubuntu读写,tomcat 标准工具,.sqlite3怎么打开,wordpress文章置顶插件:classic posts,前端拖拽布局框架话术配置,爬虫为什么会咬人手,用php写网页,seo黑帽联系,单页面淘宝客网站,网页动态滚动播放器,织梦首页模板下载lzw

向下查找子元素 用到的方法:find() children()

jsp导航网站源码,ubuntu qt读取文件,java爬虫拒绝301,PHP管材,Seo搜索指数lzw

js用的是 children[] 属性

html代码

jquery查找父元素子元素

段落1 查找父元素

段落2 查找子元素

js代码:

$(function(){

/************ 查找父元素 *************/

//closest()方法

$("#mytd1").bind("click",function(){

//alert($(this).html());

alert($(this).closest("table").attr("id")); //table1而不是table0

//alert($(this).closest("table").html());

});

//parent()方法

$("#mytd2").bind("click",function(){

//alert($(this).html()); //$(this).html()是21 (this).attr("id")是mytd2

alert($(this).parent().parent().parent().attr("id"));

//.parent()是tr 第二个.parent是tbody。即使没有tbody标签,找到的也是tbody 第三个.parent()是table

//document.write("第一个parent的id:" + $(this).parent().attr("id") + "。 第二个parent的id是:"+$(this).parent().parent().attr("id") + "。 第三个parent的id是:"+$(this).parent().parent().parent().attr("id"));

});

//parent("选择器") parents("选择器")

$("#mytd3").bind("click",function(){

$("p").parent("#div1").css("background", "yellow");//这里换成了p标签。不知道为什么用this找不到元素

//alert($(this).parent("#div").attr("id"));//undefined

alert($(this).parents("div").attr("id"));//div1 注意一个parent parents

});

/************ 查找子元素 *************/

//查找table2的td元素 find()

$("#sectd1").bind("click",function(){

alert($("#table2").find("td").length);

/* $("#table2").find("td").each(function(index,element){

alert($(element).text());

}); */

});

//children()

$("#sectd2").bind("click",function(){

var table = $("#table2");

alert($("#table2").children().children().children("td[id=sectd2]").html());

//children() 是 tbody children()是 tr children("td[id=sectd2]")是td

});

// js的 children[]

$("#sectd3").bind("click",function(){

var table = document.getElementById("table2");

alert(table.children[0].children[2].children[0].innerHTML);

//children[0] 是 tbody children[2]是 第三行的tr children[0]是td

});

});

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。