我在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/
答案 0 :(得分:0)
请记住,在jQuery中,如果要按类选择元素,可以通过
进行$('.elementClass') //Select one or more element by class
#select用于id,而不是类。
$('#elementId') //Select one element by id
希望它有所帮助。
如果您只想选择textarea。只有当它只是一个textarea。
就像这样:
$('textarea')
我仍然不会让你坚持不要使用id或类。
我解决它的方法是将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/
来自墨西哥的问候。