Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/388.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 用单击的按钮填充文本区域_Javascript_Jquery_Jquery Selectors_Jquery Events - Fatal编程技术网

Javascript 用单击的按钮填充文本区域

Javascript 用单击的按钮填充文本区域,javascript,jquery,jquery-selectors,jquery-events,Javascript,Jquery,Jquery Selectors,Jquery Events,解决 我有多个文本区和按钮外的一面。。为了在textarea中插入字符串,我使用了jQuery插件insertcaret。如何仅用文本填充上次单击的文本区域 <textarea name="answer_text[0]" cols="50" rows="3" style="width: 99%;"> AAA </textarea> <textarea name="answer_text[1]" cols="50" rows="3" style="width

解决

我有多个文本区和按钮外的一面。。为了在textarea中插入字符串,我使用了jQuery插件insertcaret。如何仅用文本填充上次单击的文本区域

<textarea name="answer_text[0]" cols="50" rows="3" style="width: 99%;">
    AAA
</textarea>

<textarea name="answer_text[1]" cols="50" rows="3" style="width: 99%;">
    BBB
</textarea>

<textarea name="answer_text[2]" cols="50" rows="3" style="width: 99%;">
    CCC
</textarea>

<textarea name="answer_text[3]" cols="50" rows="3" style="width: 99%;">
    Here should be:
    &lt;button class=insert name=insert&gt;Insert&lt;/button&gt; 
</textarea>

<button class=insert name=insert>Insert</button>

<script type="text/javascript">
jQuery.fn.extend({
insertAtCaret: function(myValue) {
    return this.each(function(i) {
        if (document.selection) {
            //For browsers like Internet Explorer
            this.focus();
            sel = document.selection.createRange();
            sel.text = myValue;
            this.focus();
        }
        else if (this.selectionStart || this.selectionStart == '0') {
            //For browsers like Firefox and Webkit based
            var startPos = this.selectionStart;
            var endPos = this.selectionEnd;
            var scrollTop = this.scrollTop;
            this.value = this.value.substring(0, startPos) + myValue + this.value.substring(endPos, this.value.length);
            this.focus();
            this.selectionStart = startPos + myValue.length;
            this.selectionEnd = startPos + myValue.length;
            this.scrollTop = scrollTop;
        } else {
            this.value += myValue;
            this.focus();
        }
    });
}
});

var pos = 0;
$('textarea').click(function() {
//Get the name of this clicked item
var pos = $(this).attr("name");
$('.insert').click(function() {
    $('textarea[name^="' + pos + '"]').insertAtCaret('Actual button code');
});
return false;
});
</script>
现在我把我点击的所有文本区域归档


我只希望填充最后一个单击的文本区域,而不是冒泡变量pos..

这是因为在文本区域单击处理程序中有.insert的单击处理程序。无论何时单击textarea,都会附加一个新的处理程序,当您单击任何textarea时,该处理程序会一直附加。把它移到外面会很好的

var pos = 0;
$('textarea').click(function() {
    //Get the name of this clicked item
    pos = $(this).attr("name");
    return false;
});
$('.insert').click(function() {
    $('textarea[name^="' + pos + '"]').insertAtCaret('Actual button code');
});