亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Contenteditable div 文本被添加到 span 元素中而不是外部

Contenteditable div 文本被添加到 span 元素中而不是外部

月關寶盒 2023-09-04 16:06:20
我的 contenteditable div 有問題。當我單擊按鈕時,一個跨度就會插入到我的 div 中。當我現在開始輸入時,文本會進入范圍內,而不是超出范圍:jQuery(document).ready(function($) {  let input = $("#input");  input.on("input", function() {    console.log($(this).html().length);        // Contenteditable adds a <br> when empty.    // Solutions on SO appear not to work    if (!$(this).text()) {      input.html('');    }  });  $("button").click(function() {    input.html(input.html() + `<span class="emoji">??</span>`);    input.trigger('input');  });});[contenteditable=true] {  border: 1px solid #aaaaaa;  padding: 8px;  border-radius: 12px;  margin-bottom: 20px;}[contenteditable=true]:empty:before {  content: attr(placeholder);  display: block;  color: #aaaaaa;}.emoji {  color: red;}<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><div id="input" placeholder="Schreib eine Nachricht..." contenteditable="true" spellcheck="true"></div><button>Add element to contenteditable div</button>這真是一件奇怪的事情。此外,當我添加表情符號、刪除內容并輸入內容時,<font>會使用表情符號的樣式創建一個元素。有誰知道我該如何解決這個問題?
查看完整描述

2 回答

?
達令說

TA貢獻1821條經驗 獲得超6個贊

只需添加contenteditable="false"您的span.


jQuery(document).ready(function($) {

  let input = $("#input");


  input.on("input", function() {

    console.log($(this).html().length);

    

    // Contenteditable adds a <br> when empty.

    // Solutions on SO appear not to work

    if (!$(this).text()) {

      input.html('');

    }

  });


  $("button").click(function() {

    input.html(input.html() + `<span class="emoji" contenteditable="false">??</span>`);

    input.trigger('input');

  });

});

[contenteditable=true] {

  border: 1px solid #aaaaaa;

  padding: 8px;

  border-radius: 12px;

  margin-bottom: 20px;

}


[contenteditable=true]:empty:before {

  content: attr(placeholder);

  display: block;

  color: #aaaaaa;

}


.emoji {

  color: red;

}

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div id="input" placeholder="Schreib eine Nachricht..." contenteditable="true" spellcheck="true"></div>

<button>Add element to contenteditable div</button>


查看完整回答
反對 回復 2023-09-04
?
qq_遁去的一_1

TA貢獻1725條經驗 獲得超8個贊

您應該使用 img 標簽來插入表情符號,我強烈建議您查看telegram web或其他基于網絡的聊天應用程序,看看您應該如何處理表情符號。



查看完整回答
反對 回復 2023-09-04
  • 2 回答
  • 0 關注
  • 337 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號