我在div后面有一个文本区域,我使用rangyinputs在该区域插入一些文本,比如周围等。
如果我使用以下代码,它会起作用:
window.surround5 = function surround5(text2,text3){
$("#textarea5").surroundSelectedText(text2, text3);
}
收到的短信是类名,但我不想使用类名,所以我尝试了:
window.surroundtest = function surroundtest(text2,text3){
var c = $(this).next('textarea');
c.surroundSelectedText(text2, text3);
}
但它一直让我对rangyinputs上的节点产生一些错误,而这些错误并没有以其他方式显示。如果我发出警报,它会显示"对象对象",所以它实际上在寻找文本区域,对吗?
实现将类似于:
<div>
<div onclick="surroundtest('[center]', '[/center]');">Center</div>
</div>
<textarea ....>
使用正在进行调用的div编辑。
JSFIDDLE:http://jsfiddle.net/qmpY8/
记住,在jQuery中,如果你想按类选择一个元素,你可以通过来完成
$('.elementClass') //Select one or more element by class
#选择器用于id,而不是类。
$('#elementId') //Select one element by id
希望能有所帮助。
如果您只想选择文本区域。并且前提是只有一个文本区域。
简单如下:
$('textarea')
我仍然无法让你坚持不要使用id或类。
我解决这个问题的方法是将id传递给函数,这样一来,html上出现多少文本区域就无关紧要了。
<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/
来自墨西哥的问候。