jquery获取子节点 求教!急!

jquery获取子节点 求教!急!,第1张

给你写个简单的 demo

<div class="boxbox clearfix">

    <span class="selected" id="boxboxa">a</span>

    <span id="boxboxb">b</span>

</div>

<div class="boxbox-con">

    <div class="onediv" id="onediv">一个div</div>

    <div class="otherdiv" id="otherdiv">另一个div</div>

</div><style type="text/css">

    clearfix:before,clearfix:after {display: table; content: ''; height: 0; width: 0; clear: both;}

    boxbox span {width: 200px; float: left; display: inline-block; height: 35px; line-height: 35px; text-align: center; font-size: 24px}

    boxbox spanselected {background-color: #f1f1f1;}

    boxbox-con {height: 300px;}

    boxbox-con onediv {background-color: red; height: 100%;}

    boxbox-con otherdiv {background-color: cyan; display: none; height: 100%}

</style><script type="text/javascript" src="jqueryjs"></script>

<script type="text/javascript">

    $('#boxboxa')on('click',function(){

        $(this)addClass('selected')siblings()removeClass('selected');

        $('#onediv')show();

        $('#otherdiv')hide();

    });

    $('#boxboxb')on('click',function(){

        $(this)addClass('selected')siblings()removeClass('selected');

        $('#onediv')hide();

        $('#otherdiv')show();

    });

</script>

这些jquery的一些查找方法:可以看看

jQueryparent(expr)           //找父元素

jQueryparents(expr)          //找到所有祖先元素,不限于父元素

jQuerychildren(expr)        //查找所有子元素,只会找到直接的孩子节点,不会返回所有子孙

jQuerycontents()            //查找下面的所有内容,包括节点和文本。

jQueryprev()                //查找上一个兄弟节点,不是所有的兄弟节点

jQueryprevAll()             //查找所有之前的兄弟节点

jQuerynext()                //查找下一个兄弟节点,不是所有的兄弟节点

jQuerynextAll()             //查找所有之后的兄弟节点

jQuerysiblings()            //查找兄弟节点,不分前后

jQueryfind(expr)            //跟jQueryfilter(expr)完全不一样,jQueryfilter(expr)是从初始的

jQuery对象集合中筛选出一部分,而jQueryfind()的返回结果,不会有初始集中

筛选出一部分,而jQueryfind()的返回结果,不会有初始集合中的内容,比如:

  $("p")find("span")是从元素开始找,等于$("p span")

var jCheckbox = jQuery('checkbox');//你的checkbox

var jTr=jCheckboxparent()parent();//即可

不知道你想获取什么jQuery很强大或者使用

var jTr=jCheckboxparents('tr');//所有父元素中选择tr

var jTr=jCheckboxclosest('tr');//只要最近的

使用jQuery框架 *** 作HTML DOM是一件极其便捷的事情,如下 *** 作即可获取相应节点的数据,id和名字(下文中引用“$(this)”表示一个jQuery对象):

获取节点标签内容数据: $(this)html( )

获取表单组件中的值: $(this)val()

获取节点中的文本内容(例如textarea): $(this)text()

获取节点的id: $(this)attr("id")

获取节点的名字: $(this)attr("name")

可以在插入c,d的代码后,重新执行一次$("a")click(function(){alert($(this)attr("name")});即可。

$("a")click(function(){alert($(this)attr("name")});是修改当前所有<a>节点的属性,此时,a,b被改变,而c和d尚未生成。

jQuery 提供了 parent() 方法用以获取当前匹配元素集合中每个元素的父元素,例如:

$("span")parent(); // 获取所有span标签元素的父节点对象集合

$("selected")parent(); // 获取所有class位selected的元素的父节点对象集合

$(spanselected)parent(); // 获取class为selected的span标签元素的父节点对象集合

另外,注意与parents() 方法的区别——parents() 方法获取当前匹配元素集合中每个元素的祖先元素(祖先元素不仅包括父节点,还包括祖父、曾祖父等等)。

以上就是关于jquery获取子节点 求教!急!全部的内容,包括:jquery获取子节点 求教!急!、如何使用Jquery获取当前节点的孙节点、jquery获取父节点的节点等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9878330.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-02
下一篇2023-05-02

发表评论

登录后才能评论

评论列表(0条)

    保存