Javascript 如何将下一个文本区域集中在按钮单击上

Javascript 如何将下一个文本区域集中在按钮单击上,javascript,jquery,html,Javascript,Jquery,Html,我试图做一些类似于社交网络的事情,但是我在jquery上遇到了问题,我想,通过单击comment按钮,用户被带到comment字段,但是我不能使用$(this) 当用户单击此处时 守则: <button type="button" class="btn btn-default abreComentarios" > <span class="fa fa-comments-o"></span> </button> 记住,我使用的是fo

我试图做一些类似于社交网络的事情,但是我在jquery上遇到了问题,我想,通过单击comment按钮,用户被带到comment字段,但是我不能使用
$(this)

当用户单击此处时



守则:

 <button type="button" class="btn btn-default abreComentarios" >
   <span  class="fa fa-comments-o"></span>
 </button>
记住,我使用的是foreach,所以我必须使用
$(this)

你的
next()
不是
.caixaComentario
而是
.comentar

因此,请使用
next()
,然后必须使用
find()
(或
children()
)来聚焦文本区域

$('.abreComentarios')。在('click',function()上{
//console.log('entrou');
$(this.next('.comentar').find('.caixaComentario').focus();
})

点击
你的
next()
不是
.caixaComentario
而是
.comentar

因此,请使用
next()
,然后必须使用
find()
(或
children()
)来聚焦文本区域

$('.abreComentarios')。在('click',function()上{
//console.log('entrou');
$(this.next('.comentar').find('.caixaComentario').focus();
})

点击
$(这)将是您的
,但正在调用。下一步(“.caixaComentario”)将查找按钮的同级元素。如果您的
是同级元素,则.next(“.caixaComentario”)将不匹配任何元素,因为它们不是同级元素。他可能是侄女/侄子

尝试将.next(“.caixaComentario”)更改为.next(.div.caixaComentario”)

$(这将是您的
,但是调用。next(.caixaComentario”)将查找按钮的同级元素。如果您的
是同级元素,则.next(“.caixaComentario”)将不匹配任何元素,因为它们不是同级元素。他可能是侄女/侄子

尝试将.next(“.caixaComentario”)更改为.next(“div.caixaComentario”)

解决了,我刚刚完成了:

1-在按钮中添加带有帖子id的数据id

2-在类“caixaComentario”的名称末尾添加了相同的id


3-在jQuery上不使用$(this)调用

$('body')。在('click','abreComentarios',function()上{
var id=$(this.data(“id”);
$('.caixaComentario'+id).focus();
});

工作:解决了,我就这么做了:

1-在按钮中添加带有帖子id的数据id

2-在类“caixaComentario”的名称末尾添加了相同的id


3-在jQuery上不使用$(this)调用

$('body')。在('click','abreComentarios',function()上{
var id=$(this.data(“id”);
$('.caixaComentario'+id).focus();
});


工作正常:D

您的控制台有错误吗?@VitorZF您提供的代码中没有foreach…没有错误,如果我把重复的整个代码都放在这里,那么就会有两页这样的内容question@cmprogram看看你的控制台有没有错误?@VitorZF你提供的代码中没有foreach…没有错误,如果我把重复的整个代码都放在这里,那么就会有两页这样的内容question@cmprogram看一看,或者可能是
最接近的()
不,
最接近的()
向上而不是向下:
给定一个表示一组DOM元素的jQuery对象,.closest()方法在DOM树中搜索这些元素及其祖先,并从匹配的元素构造一个新的jQuery对象。
它在FIDLE中工作,而不是将代码作为图像或在google驱动器中发布,您需要发布问题中的相关代码,但要发布生成的html,而不是PHP代码,或者可能是
.closest()
nop,
closest(),您需要在问题中发布相关代码,而不是将代码作为图像或在google驱动器中发布,而是发布生成的html,而不是PHP代码
<div class="comentar">
    <textarea class="txtComentario form-control caixaComentario" placeholder="Seu comentário" onkeypress="comentarEnter()"></textarea>
</div>
$('body').on('click', '.abreComentarios', function() {
      //console.log('entrou');
    $(this).next('.caixaComentario').focus();
 });