直接上 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/
图片演示:
将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);