javascript - 父元素选择问题?

标签 javascript jquery jquery-selectors

我的 HTML 是这样的

<div id="mydiv" class="common">
      <input type="text" id="text1" value="" />
      <input type="text" id="text2" value="" />
</div>

我正在像这样在文本框的 onclick 事件上分配一个函数

$(document).ready(function() {
      $(".common input").click(function() {

          //////// What I am trying to do is access the id of its parent 
                // in this case it is "mydiv"
          alert($(this:parent).attr('id'));
});

但是没有效果

最佳答案

试试 $(this).parent().attr('id');

关于javascript - 父元素选择问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3003788/

相关文章:

javascript - 隐藏 Vimeo 视频中的选项

javascript - 将变量传递到 jQuery AJAX

javascript - 使用 Jquery 检测选择列表的更改

javascript - 在API中发出请求并计算结果

javascript - Lightbox 不加载图像。卡在加载部分

javascript - 遍历元素列表并隐藏它们

javascript - 变量似乎是耦合的

jQuery 第 n 个子级选择器

javascript - 属性选择器不起作用(语法错误,无法识别的表达式)

javascript - 滚动位置如何保持在侧边栏中的事件项目上?