Jquery 无法获取活动div的内容
单击内部Jquery 无法获取活动div的内容,jquery,contenteditable,Jquery,Contenteditable,单击内部ed它将变为活动状态-(蓝色边框环绕) 单击btnsave不会给出ed的内容,而是给出整个文档的内容 $("#btnsave").click(function(){ var a = document.activeElement.innerText; console.log(a); }); 我想这样就可以了 另一种解决方案: $("#btnsave").click(function(){ var a = $('.ed:focus').text(); }); 我
ed
它将变为活动状态-(蓝色边框环绕)
单击btnsave
不会给出ed
的内容,而是给出整个文档的内容
$("#btnsave").click(function(){
var a = document.activeElement.innerText;
console.log(a);
});
我想这样就可以了
另一种解决方案:
$("#btnsave").click(function(){
var a = $('.ed:focus').text();
});
我想这样就可以了
另一种解决方案:
$("#btnsave").click(function(){
var a = $('.ed:focus').text();
});
我将save元素更改为一个按钮,使其能够正常工作:
洛雷姆·洛雷姆·洛雷姆
同侧同侧
拯救
$(“#btnsave”)。单击(函数(){
var a=getSelectionStart();
控制台日志(a);
});
函数getSelectionStart(){
var node=document.getSelection().anchorNode;
console.log(节点);
返回(node.nodeType==3?node.parentNode:node);
}
要归功于此我将save元素更改为一个按钮,使其能够正常工作:
洛雷姆·洛雷姆·洛雷姆
同侧同侧
拯救
$(“#btnsave”)。单击(函数(){
var a=getSelectionStart();
控制台日志(a);
});
函数getSelectionStart(){
var node=document.getSelection().anchorNode;
console.log(节点);
返回(node.nodeType==3?node.parentNode:node);
}
这归功于此
$('.ed').text()
仅用于文本,或$('.ed').html()
用于文本和其他htmlobject@guradio,谢谢,但我需要活动div的内容。下一次单击将在另一个div上。我已更新了我的答案。@RinoRaj,您更新的答案无效。控制台是空的。发布前请检查您的解决方案。@bonaca请检查我的演示$('.ed').text()
,查看纯文本或$('.ed').html()
,查看文本和其他htmlobject@guradio,谢谢,但我需要活动div的内容。下一次单击将在另一个div上。我已更新了我的答案。@RinoRaj,您更新的答案无效。控制台是空的。请在发布之前检查您的解决方案。@bonaca检查我的demoRino,谢谢,但我需要活动div的内容。下一次单击将在另一个div上。文档中有许多ed
div。否,控制台为空,带有var a=$('.ed:focus').text()代码>为什么不能使用html()而不是text()@safinchacko html()将返回全部内容。考虑一下,如果OP在那个div中有更多的元素,那么HTML-()不太好:RINO,谢谢,但是我需要激活div的内容。下一个点击将在另一个div上。文档中有许多代码< ED>代码> div。代码>为什么不能使用html()而不是text()@safinchacko html()将返回全部内容。考虑一下,如果OP在那个div中有更多的元素,那么HTML-()不太好:
$("#btnsave").click(function(){
$('.lastEdited').text();
alert($('.lastEdited').text())
});
$("div[contentEditable]").click(function(){
$("div[contentEditable]").removeClass('lastEdited');
$(this).addClass('lastEdited');
});
<div class="wrap3">
lorem lorem lorem
<div class="inside3 ed" contentEditable>ipsum ipsum ipsum</div>
</div>
<button id="btnsave">Save</button>
$("#btnsave").click(function(){
var a = getSelectionStart();
console.log(a);
});
function getSelectionStart() {
var node = document.getSelection().anchorNode;
console.log(node);
return (node.nodeType == 3 ? node.parentNode : node);
}