【问题标题】:Not being able to get next textarea working无法让下一个 textarea 工作
【发布时间】:2013-08-12 21:13:33
【问题描述】:

我在 div 之后有一个 textarea,我正在使用 rangyinputs 在该区域插入一些文本,例如周围等。

如果我使用以下代码,它可以工作:

window.surround5 = function surround5(text2,text3){
$("#textarea5").surroundSelectedText(text2, text3);
}

作为 textarea5 类名,但我不想使用类名,所以我尝试了:

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

但它不断让我对 rangyinputs 上的节点出现一些错误,而这些错误并没有以其他方式显示。如果我发出警报,它会显示“对象对象”,所以它实际上找到了 textarea 吗?

实现将是这样的:

<div>
<div onclick="surroundtest('[center]', '[/center]');">Center</div>
</div>
<textarea ....>

使用正在调用的 div 进行编辑。

JSFIDDLE:http://jsfiddle.net/qmpY8/

【问题讨论】:

  • 你能告诉我们你正在处理的html吗?
  • $(this).parent().next('textarea'); 是我想的你
  • Orange 也试过了,没用。 @BetoCastillo 就像上面的示例一样,一些 div 和一个在中间进行调用的。并且 textarea 在结束 div 之后
  • 我没有在您的 html 代码中看到任何类或 id 定义。如果你使用类或 id 会更容易。
  • @BetoCastillo 因为我不想使用它,所以我将克隆整个代码,所以我不想命名文本区域。我将编辑调用的 div。

标签: php jquery textarea


【解决方案1】:

记住,在 jQuery 中,如果你想按类选择一个元素,你可以这样做

$('.elementClass') //Select one or more element by class

# 选择器用于 id,而不是类。

$('#elementId') //Select one element by id

希望对你有帮助。

如果你只想选择文本区域。并且仅当只是一个文本区域。

就像这样:

$('textarea')

我仍然没有让你坚持不要使用 id 或 class。

我解决它的方法是将 id 传递给函数,因此,这样,html 上出现多少 textareas 并不重要。

<div>
<div class="guides_chapters_button" onclick="surround('[center]', '[/center]', 'textareamatch');">Center</div>
<div class="guides_chapters_button" style="text-decoration: underline;" onclick="surround('[u]','[ u/]', 'textareamatch');">u</div>

window.surround = function surround(text2,text3, id){
    $("#" + id).surroundSelectedText(text2, text3);
}

我测试过,它是有效的。 http://jsfiddle.net/wNrWC/

来自墨西哥的问候。

【讨论】:

  • 我不想按类或ID选择,我想选择下一个textarea。
  • 谢谢,我想我没有任何其他选择:x 现在的问题是,当我克隆它时,我必须将 onclick 上的文本编辑为新的 id。这会很复杂。
猜你喜欢
  • 1970-01-01
  • 2018-02-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-11
  • 1970-01-01
  • 1970-01-01
  • 2011-09-01
相关资源
最近更新 更多