首页 新闻 搜索 专区 学院

关于Focus 光标定位在Safari和Chrome 上不一致的问题

0
悬赏园豆:20 [已解决问题] 解决于 2018-03-07 17:02

直接上 Demo:

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

在Safari 上和在Chrome上效果不一致!在Safari上光标跑到最前面了。

Demo 地址:https://jsfiddle.net/butterapple/bm1qm1ut/
图片演示:

BUTTERAPPLE的主页 BUTTERAPPLE | 老鸟四级 | 园豆:2950
提问于:2018-03-07 16:49
< >
分享
最佳答案
0

将Javascript代码改成下面这样解决:

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);
}

$("#qCommentTextarea").focus().val(replyTo);

BUTTERAPPLE | 老鸟四级 |园豆:2950 | 2018-03-07 17:02
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册