2017-09-21 82 views
2

我有以下的html:如何获取内容可编辑div的新文本?

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

然后我做的:

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

的呼叫:当我改变对格文

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

然而,控制台剧照给我老的文本,而不是新的

+0

把初始化内函数'var termS;' – guradio

+0

我给它另一种尝试却是行不通的,因为它没有拿起新的文本改变 –

+0

在https://jsfiddle.net/84n6tdm1/ –

回答

0

它工作正常的片断:

$("#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>

+0

是啊..但没有工作就在这里,我要去试试这个https://stackoverflow.com/a/6256386/1018804 –

0

上的事件只需添加jQuery的ready事件里面像这样:

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

和HTML为你做:

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