使用jquery获取动态创建的元素的子元素的.html?

使用jquery获取动态创建的元素的子元素的.html?,jquery,Jquery,我正在尝试使用.on'click'获取post content-[[!+post.id]]的.html,但是控制台一直告诉我没有.parent.previous的函数 我的HTML <div class="post-wrapper"> <div class="post-header"> <div class="post-avatar floatLeft"> <a href=""><img cla

我正在尝试使用.on'click'获取post content-[[!+post.id]]的.html,但是控制台一直告诉我没有.parent.previous的函数

我的HTML

<div class="post-wrapper">
    <div class="post-header">
        <div class="post-avatar floatLeft">
            <a href=""><img class="miniCircle" src="[[!+post.photo]]"></a>
        </div>
        <div class="post-info floatLeft">
            <span class="in-line"><a href="">[[!+post.owner]]</a></span>
            <span class="in-line"><time class="timeago smallFont" datetime="[[!+post.date]]"></time></span>
        </div>
        <div class="clear"></div>

    </div>
    <div class="post-content">
        <div id="post-content-[[!+post.id]]" class="post-content i-content">[[!+post.content]]</div>
    </div>
    <div class="post-footer">
        [[!+post.links]]
        <span class="ui-icon ui-icon-star" style="display: inline-block" title="Like">&nbsp;</span>
    </div>
</div>
如何消除TypeError:$..parent….previous不是一个函数,在不按id引用的情况下获取post content-[!+post.id]]中的内容

通过ID获取该div的html内容只在第一个页面加载时起作用,然后一旦我的ajax更新了内容,即使我准确地使用了新ID,它也将无法定位,因为DOM已经更改。但是我知道如果我使用相对定位,我可以得到html内容,因为我在一个教程中看到了它,但是该教程只对带有a及其子元素的单层元素集进行了示例。

它应该是,但是,这只会向下遍历一行

给定一个表示一组DOM元素的jQuery对象,.children方法允许我们在DOM树中搜索这些元素的子元素,并从匹配的元素构造一个新的jQuery对象。.children方法与.find方法的不同之处在于.children只沿DOM树向下移动一个级别,而.find可以向下移动多个级别以选择后代元素、孙子辈等。还要注意,与大多数jQuery方法一样,.children不返回文本节点;要获取包括文本和注释节点在内的所有子节点,请使用.contents

或者,您可以尝试通过引用子对象

在文档中是一个很好的参考,您可能会发现一些我没有列出的替代选项。不过,previous应该是prev!至于回答这个问题

这是我很久以前学习jQuery时做的一个实现的参考,我还不是专家,我做SuiteScript/Automation/Business Logic:

    $(".title").click(function(){
    var height = $(this).parent().children(".content").children("p").height();
    if($(this).parent().children(".content").height() == 0){
        height = height.toString() + 'px';

        /* Reset styling of all posts */
        $(".title").css("color","black");
        $(".content").css({"height": "0px", "color": "#5a5858"});

        /* Set this post's styling */
        $(this).css('color','white');
        $(this).parent().children(".content").css({'height': height, 'color':'white'});
    /* On unclick */    
    } else {
        $(this).css('color', 'black');
        $(this).parent().children(".content").css({'height': '0px', 'color':'#5a5858'});
    }
    });
它应该是,但是,这只会向下穿过一条线

给定一个表示一组DOM元素的jQuery对象,.children方法允许我们在DOM树中搜索这些元素的子元素,并从匹配的元素构造一个新的jQuery对象。.children方法与.find方法的不同之处在于.children只沿DOM树向下移动一个级别,而.find可以向下移动多个级别以选择后代元素、孙子辈等。还要注意,与大多数jQuery方法一样,.children不返回文本节点;要获取包括文本和注释节点在内的所有子节点,请使用.contents

或者,您可以尝试通过引用子对象

在文档中是一个很好的参考,您可能会发现一些我没有列出的替代选项。不过,previous应该是prev!至于回答这个问题

这是我很久以前学习jQuery时做的一个实现的参考,我还不是专家,我做SuiteScript/Automation/Business Logic:

    $(".title").click(function(){
    var height = $(this).parent().children(".content").children("p").height();
    if($(this).parent().children(".content").height() == 0){
        height = height.toString() + 'px';

        /* Reset styling of all posts */
        $(".title").css("color","black");
        $(".content").css({"height": "0px", "color": "#5a5858"});

        /* Set this post's styling */
        $(this).css('color','white');
        $(this).parent().children(".content").css({'height': height, 'color':'white'});
    /* On unclick */    
    } else {
        $(this).css('color', 'black');
        $(this).parent().children(".content").css({'height': '0px', 'color':'#5a5858'});
    }
    });

.previous不存在,请使用prev,也不要使用.genderant,您可以使用.children或find


,它告诉您不是一个函数,因为它实际上不存在,也不是因为您的代码中有任何错误。

.previous不存在,请使用prev和.degenant,您也可以使用.children或find

,它告诉您它不是一个函数,因为它实际上并不存在,也不是因为代码中有任何错误。

试试看 console.log'content:'+$this.parent.prev.children.html

试试看
console.log'content:'+$this.parent.prev.children.html

jQuery中没有.substant方法。你的意思是。找到?和。上一个应该是。上一个。这些不存在的函数名是从哪里来的?另外,jQuery中没有以前的方法。你是说,上一个?你需要手册!我只是在学习,请原谅我的无知。它确实与名为的正确函数一起工作。我在我的IDE中输入了.previous,它的颜色是一个函数关键字,所以我觉得我弄坏了其他东西。哦,jQuery中没有.substant方法。你的意思是。找到?和。上一个应该是。上一个。这些不存在的函数名是从哪里来的?另外,jQuery中没有以前的方法。你是说,上一个?你需要手册!我只是在学习,请原谅我的无知。它确实与名为的正确函数一起工作。我在我的IDE中输入了.previous,它的颜色是一个函数关键字,所以我觉得我弄坏了其他东西。哦,谢谢你!我经常引用文档,但这次我假设这些是函数,因为IDE中的代码着色。我应该先回到文档中,谢谢!我经常引用文档,但这次我假设这些是函数,因为IDE中的代码着色。我应该先回到文档。谢谢你的链接和回答,但我把这篇文章交给了@specrte-d,因为它包含了文档引用和更完整的回答。谢谢你的链接
和回答,但我把这个给了@specrte-d,因为它包含了文档引用和更完整的答案。谢谢你的回答,但我把这个给了@specrte-d,因为它包含了文档引用和更完整的答案。谢谢你的回答,但我把这个给了@specrte-d,因为它包含了文档引用,还有一个更完整的答案。