代码之家  ›  专栏  ›  技术社区  ›  mark vanzuela

jquery-如何在DIV中获取HTML

  •  1
  • mark vanzuela  · 技术社区  · 15 年前

    我有这个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头部分的按钮后,如何获取HTML,在本例中是表标记?

    我有这个jquery代码来获取标题上的文本: $(this).parent().text(); 但是在上面所示的“Content-Regular”类下尝试检索HTML却毫无进展。

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

    谢谢

    3 回复  |  直到 14 年前
        1
  •  3
  •   Nick Craver    15 年前

    使用 .next() 从中得到 .portlet-header .portlet-content 然后使用 .html() 而不是 .text() 要获取内容,请执行以下操作:

    $(".portlet-header").click(function() {
      var html = $(this).next(".portlet-content").html();
    });
    

    如果分隔符、另一个元素等之间可能存在某些内容,请使用 .nextAll() 而是这样: .nextAll(".portlet-content:first")

        2
  •  3
  •   Peter Kruithof    15 年前

    首先,使用 html() 函数而不是 text() 功能。其次,将选择器指向右节点,类似于 .portlet-content .content_regular table . 我让你自己把这两个结合起来。

        3
  •  1
  •   Reigel Gallarde    15 年前
    $('.portlet-header button').click(function(){
       alert($('.content_regular').html())
    });
    

    好读物

    1. .html()
    2. jQuery selectors
    3. Traversing

    here is a demo