获取没有id的div外的下一个textarea

Posted

技术标签:

【中文标题】获取没有id的div外的下一个textarea【英文标题】:Get next textarea outside div without id 【发布时间】:2013-08-13 13:41:49 【问题描述】:

我需要获取下一个 textarea,但我无法使用 next 甚至 find。

示例代码 html

<div>
    <div class="guides_chapters_button" onclick="surroundtest('[center]', '[/center]');">Center</div>
    <div class="guides_chapters_button" style="text-decoration: underline;" onclick="surround('[u]', '[/u]');">u</div>
</div>
<textarea class="guides_chapters_textarea" id="textareamatch" name="matchupm" rows="7" cols="25"></textarea>

JS:

window.surround = function surround(text2,text3)
$("#textareamatch").surroundSelectedText(text2, text3);

function surroundtest(text2,text3)
var c = $(this).parent().next('textarea');
c.surroundSelectedText(text2, text3);

JS FIDDLE:http://jsfiddle.net/qmpY8/1/

我需要使用的是环绕测试,另一个是使用 id 的示例。我很想替换那个,因为我正在使用克隆对象。

【问题讨论】:

【参考方案1】:

surroundtest 中的 this 语句适用于 window 对象而不是元素。您应该做的是将函数定义更改为:

function surroundtest(element, text2,text3)
    var c = $(element).parent().next('textarea');
    ...

相应的 HTML:

<div class="guides_chapters_button" onclick="surroundtest(this, '[center]', '[/center]');">Center</div> 

【讨论】:

如何解决换行问题?这就是我使用另一个窗口的原因。【参考方案2】:

如果这是您要使用的 HTML,那么 .closest() 也可以用于获取 textarea 元素。如下所示:

var c = $(element).parent().closest('textarea');

【讨论】:

以上是关于获取没有id的div外的下一个textarea的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 jquery 找到 div 的下一个跨度? [关闭]

如何根据数组中的下一个/最后一个值更改 div id 的值

jQuery常用知识点汇总

jquery 获取名为 moreProductsBox 的下一个 div 并滑动打开它

如何获取在jquery中可见的下一个元素

vue使用textare如何正确统计输入字符个数