Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jquery-如何在div中获取html_Jquery_Traversal - Fatal编程技术网

jquery-如何在div中获取html

jquery-如何在div中获取html,jquery,traversal,Jquery,Traversal,我有这个html: <div class="portlet-header"> Company Information ... <button> some button here </button> </div> <div class="portlet-content"> <div class="content_regular"> <table style=" width:100%; he

我有这个html:

<div class="portlet-header">
    Company Information ... <button> some button here </button>
</div>
<div class="portlet-content">
    <div class="content_regular">
        <table style=" width:100%; height:100%;">
            ...content
        </table>
    </div>
</div>

公司信息。。。这里有按钮吗
…内容
单击portlet标题部分的按钮(在本例中为table标记)后,如何获取html

我使用jquery代码获取标题上的文本:
$(this.parent().text()
但在试图检索“content\u regular”类下的html时却一无所获,如上图所示

希望你的智慧能帮助我。我知道这对其他人来说很容易,但我不熟悉jquery

谢谢

首先,使用
html()
函数代替
text()
函数。其次,将选择器指向右侧节点,类似于
.portlet content.content\u常规表
。我将让您自己将这两者结合起来。

用于从
获取portlet头
。portlet内容
然后使用而不是获取内容,如下所示:

$(".portlet-header").click(function() {
  var html = $(this).next(".portlet-content").html();
});
如果div、另一个元素等之间可能存在某些内容,请改为使用:
.nextAll(“.portlet内容:first”)

好书


  • 是的,我可以使用它,但我想知道的是如何从portlet头遍历到content\u regular div。我需要的是content_正则div下的hmtl,如我所说,更改选择器并将其指向正确的节点。简单的方法是使用类名本身。但是,如果该类被多次使用,则必须从单击的目标进行遍历。如果是这种情况,从目标($(this))开始,上升一级(portlet头),找到下一个同级(portlet内容)并查看内部以获得表。@Peter-您需要遍历…选择器不能完成此工作,它们是不同的概念。感谢您的输入,它为我的小大脑提供了光明。我确实使用了选择器样式。保存我的a**的是上面的代码由一个div包装,该div对每个portlet都有唯一的id。所以我所做的是这样的var divId=$(this.parent().parent().attr(“id”);var contenthtml=$(“#”+divId+“.contentw_regular”).html()@mcxiand-您不必这样做…我的答案为您提供了一个非常清晰的获取所需div的路线,并且获得它所需的工作量要少得多。$('.content_regular').html()确实有效,但这里的问题是,上面的代码是一个小部件。同一页上至少有4个。如果我使用$('.content_regular').html(),则只返回第一个。@mcxind考虑到您的多个小部件和基于相对标题的选择,这是最有意义的。如果你在任何点击中都需要它们,你也可以使用.each并将它们放在一个数组中。我同意这是最有意义的,但是我似乎无法让它工作。我的代码是这样的,$(this.parent().next().nextAll(“.contentwithscroll\u regular:first”).html()-我应该调用parent,因为该按钮位于portlet标题中-然后下一步转到portlet标题到portlet内容-我现在不知道如何在调用html()之前从portlet内容遍历到内部div内容(明白了!)$(this.parent().next().children(“.contentwithscroll_regular”).html();
    $('.portlet-header button').click(function(){
       alert($('.content_regular').html())
    });