在文本文件中外化typeInTextarea的内容

问题描述 投票:1回答:1

我目前正在尝试在文本文件中外化typeInTextarea的内容。我的想法是,当我单击一个按钮时,文本文件的所有内容都进入光标所在的文本区域(我不希望新内容删除旧内容)...我使用该代码:

function typeInTextarea(el, newText) {
  var start = el.prop("selectionStart")
  var end = el.prop("selectionEnd")
  var text = el.val()
  var before = text.substring(0, start)
  var after  = text.substring(end, text.length)
  el.val(before + newText + after)
  el[0].selectionStart = el[0].selectionEnd = start + newText.length
  el.focus()
  return false
}
$("button").on("click", function() {
  typeInTextarea($("textarea"), "  SOME TEXT  ")
  return false
})

而我正在努力实现这样的目标:

$("button").on("click", function() {
  typeInTextarea($("textarea"), "  SOMETEXT.txt  ")
  return false
})
javascript jquery cursor textarea external
1个回答
0
投票

//已解决//使用php

我的PHP

<?php
$file3 = file_get_contents('media/test3.txt');
$file4 = file_get_contents('media/test4.txt');
...
...
?>

我的HTML

<a href="" data-text="<?php echo $file3 ?>">Test 1</a> test3<br/>
<a href="" data-text="<?php echo $file4 ?>">Test 2</a> test4<br/>
<textarea rows="5" cols="40" name="replycontent" id="replycontent"></textarea>

我的js

<script type="text/javascript">
    $("a[data-text]").click(function(){
  var value = $("#replycontent").val();
   $("#replycontent").val(value+$(this).attr("data-text"));
   return false;
})
</script>

PHP总是在帮助

© www.soinside.com 2019 - 2024. All rights reserved.