jquery - 单击时显示子 div 属性

标签 jquery html

这无疑是一个基本的 jQuery 问题,但我有一个像这样的 HTML div 集群:

<div class="sunti_contain" ng-repeat="sunti in suntis track by $index">
                <div class="individual_sunti">
                    <!--needs a unique div#id via angularz-->
                    <div class="sunti_content">{{sunti.content || 'content loading...'}}</div>
                    <div class="sunti_tags">{{sunti.tags || 'tags'}}</div>
                    <div class="sunti_author">{{sunti.author || 'author-placeholder'}}</div>
                    <div class="sunti_shortid">{{sunti.shortid}}</div>
                </div>
            </div>

({{ }}是有角度的填充,但它们对于这个问题并不重要)

在父元素 .individual_sunti 上的单击事件中,我想将子元素 sunti_shortid 值写入控制台。

到目前为止我已经:

$('.individual_sunti').click(function() {
        console.log('caught a click on ' + $(this));
}

但我不知道要向 $(this) 添加什么来引用名为 .sunti_shortid 的子元素

像天真的东西

    console.log('caught a click on ' + $(this > '.sunti_shortid'));

不起作用

最佳答案

使用.find():

$(this).find('div.sunti_shortid');

.children():

$(this).children('div.sunti_shortid');

区别在于 .children() 仅检查(等等...)子元素(即向下一级),而 .find() 搜索所有后代。

关于jquery - 单击时显示子 div 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39782691/

相关文章:

jquery - 有没有办法不查看 Rails 中的操作?

jquery - 如何使用 Devise 和 activerecord-reputation-system 获取 current_user 进行 Ajax 投票

html - 如何使用 html css bootstrap 创建侧边栏菜单和可点击的子菜单?

html - 具有嵌入式视频的响应式 2 列布局

Jquery cookie 监控

jQuery:<Select> 表单帮助

html - 如何使 div 适合较小屏幕的屏幕边缘?

html - 使用 th :href 在 spring mvc 中引用带有 thymeleaf 的 .css 文件

javascript - 如何显示在密码输入类型中输入的文本,否则隐藏密码

javascript - AngularJS $animateCss 在页面加载和 View 更改上