光标定位Demo

by Yin Xi

HTML

<textarea id="qCommentTextarea" class="question-comment-textarea" cols="40" rows="3" placeholder="输入内容"></textarea>
<a class="question-comment-reply clear" id="qcu_1" onclick="AddQuestionComment(1, '111');" title="回复此评论">回复</a>

CSS

.clear {
  clear: both;
  display: block;
}

JavaScript

function AddQuestionComment(cid, currUser) {
  var replyTo = "";
  var username = "罗伯特";
  if ($("#qCommentEditor").css("display") === "none") {
    if (cid !== "") {
      if (username === currUser) {
        username = "";
      }
      if (username !== "") replyTo = "@" + username + ": ";
      qCommentId = cid;
    }
    $("#qCommentEditor").attr("style", "display:unset;");
  } else {
    if (cid !== undefined) {
      if (username === currUser) {
        username = "";
      }
      if (username !== "") replyTo = "@" + username + ": ";
      qCommentId = cid;
    } else {
      $("#q_comment").text("评论");
      $("#qCommentEditor").attr("style", "display:none;");
    }
  }
  $("#qCommentTextarea").focus().val(replyTo);
}