Javascript 在span文本后添加不在span标记内的文本
检查此屏幕截图: 我用这段代码在DIV中添加表情符号的跨度Javascript 在span文本后添加不在span标记内的文本,javascript,jquery,html,css,Javascript,Jquery,Html,Css,检查此屏幕截图: 我用这段代码在DIV中添加表情符号的跨度 $('body').on('click', '.selectable-icons', function(e) { if (e.target.tagName.toLowerCase() === 'span') { document.querySelector('[contenteditable]').appendChild(e.target.cloneNode(true)); $('[conten
$('body').on('click', '.selectable-icons', function(e) {
if (e.target.tagName.toLowerCase() === 'span') {
document.querySelector('[contenteditable]').appendChild(e.target.cloneNode(true));
$('[contenteditable]').append(" ");
var current_text = $('[contenteditable]').html();
alert(current_text);
$('.publisher_div').html(current_text);
}
但是span
标记后面没有空格,因此我可以在span
标记后面写下一个文本。试试:
$('[contenteditable]').append(" ");
尝试:
$('[contenteditable]')。追加(“”)
将使用contenteditable
属性(在您的示例中是span
)向元素追加一个空格。如果要在之后添加空格,请使用:
实例:
var container=$(“#container”);
console.log(“之前:”);
container.contents().each(函数(索引){
console.log(索引,this.nodeType,this.nodeType==3?'“+this.nodeValue+'”:this.tagName);
});
$(“[contenteditable]”),在(“”)之后;
console.log(“之后:”);
container.contents().each(函数(索引){
console.log(索引,this.nodeType,this.nodeType==3?'“+this.nodeValue+'”:this.tagName);
});代码>
这是跨度
$(“[contenteditable]”)追加(“”)
将使用contenteditable
属性(在您的示例中是span
)向元素追加一个空格。如果要在之后添加空格,请使用:
实例:
var container=$(“#container”);
console.log(“之前:”);
container.contents().each(函数(索引){
console.log(索引,this.nodeType,this.nodeType==3?'“+this.nodeValue+'”:this.tagName);
});
$(“[contenteditable]”),在(“”)之后;
console.log(“之后:”);
container.contents().each(函数(索引){
console.log(索引,this.nodeType,this.nodeType==3?'“+this.nodeValue+'”:this.tagName);
});代码>
这是跨度
试试这个
$('.publisher_div').append("hi");
试试这个
$('.publisher_div').append("hi");
你能提供一把小提琴吗?你试过使用
?@hallleron:堆栈片段,而不是小提琴。你能提供一把小提琴吗?你试过使用
?@hallleron:堆栈片段,而不是小提琴。“在span文本之后添加文本…不在span标记内”append
added in in。“在span text之后添加文本…不在span标记内”append
adds in。