在 jquery 中将最近元素的 id 作为参数值传递



我有几个参数传递给js函数(我正在使用jeditable(。

$('.edit_area').editable('update.php',{
id      : '2',
type      : 'textarea'
});

.edit_area是一个<p>元素。我需要将"id"的值作为 最接近的父元素<div>的值。

任何帮助将不胜感激。谢谢!

如果页面中只有一个.edit_area元素

$('.edit_area').editable('update.php', {
    id: $('.edit_area').closest('div').att('id'),
    type: 'textarea'
});

如果您有多个元素

$('.edit_area').each(function(){
    var $this = $(this);
    $this.editable('update.php', {
        id: $this..closest('div').att('id'),
        type: 'textarea'
    });
})

使用 closest(( 获取父级层次结构中的第一个div。

$('.edit_area').editable('update.php',{
   id    : $('.edit_area').closest('div').attr('id'),
   type  : 'textarea'
});

最接近((

对于集合中的每个元素,获取与 通过测试选择器 元素本身并在 DOM 树中向上遍历其祖先,参考。

试试

$('#2').parents('div:eq(0)').attr('id');  //where 2 is your param

最新更新