简体   繁体   English

如何获取内容可编辑div的新文本?

[英]How to get the new text of a content editable div?

I have the following html: 我有以下html:

<div class="custom_title" contenteditable="true" maxlength="50">
   WRITE YOUR CUSTOM TITLE HERE
</div>

Then I do: 然后我做:

$("#search_wiki").on("click", function() {
  myGoogle();
});

Which calls: 哪个调用:

var termS;  
function myGoogle() {
  termS = $(".custom_title").html();
  console.log(termS);

Yet when I change the text on that div, the console stills gives me the old text and not the new one 但是,当我在该div上更改文本时,控制台仍然会给我旧文本而不是新文本

It works fine in the snippet: 在代码段中工作正常:

 $("#search_wiki").on("click", function() { myGoogle(); }); var termS; function myGoogle() { termS = $(".custom_title").html(); console.log(termS); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="custom_title" contenteditable="true" maxlength="50"> WRITE YOUR CUSTOM TITLE HERE </div> <h1 id="search_wiki">Search wiki</h1> 

Just add jQuery on events inside ready event like this: 只需在ready事件中的事件上添加jQuery,如下所示:

<script src= "https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  $(document).ready(function() {
    $("#search_wiki").on("click", function() {
      myGoogle();
    });
  });

and the html as you made: 和您制作的html:

<div class="custom_title" contenteditable="true" maxlength="50">
  WRITE YOUR CUSTOM TITLE HERE
</div>
<h1 id="search_wiki">Search wiki</h1>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM