如何访问父Div的第一个子Div [英] How to Access First Child Div of the Parent Div

查看:345
本文介绍了如何访问父Div的第一个子Div的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在div的click事件上,我正在尝试为该div父div的第一个孩子的文本值分配一个变量.例如,html是这样的:

On a click event for a div, I'm trying to assign a variable the text value of the first child of that divs parent div. For example, the html is something like this:

<div class="parentDiv">
    <div class="firstChild">
        1234
    </div>

    <div class="secondChild">
        Hello
    </div>

    <div class="thridChild">
        Bye
    </div>

</div>

因此,只要单击父div内的任何内容,我都希望它为firstChild div的文本值分配一个变量.您将如何去做?

So I want it to assign a variable the text value of the firstChild div whenever the click on anything inside the parent div. How would you go about doing this?

推荐答案

您可以使用:

var text = $('.parentDiv:nth-child(1)').html();

这篇关于如何访问父Div的第一个子Div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆