将换行符仅添加到textContent或innerText - 在Chrome中 [英] Add linebreak to textContent or innerText only - in Chrome

查看:447
本文介绍了将换行符仅添加到textContent或innerText - 在Chrome中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个复杂的!我在Chrome中使用contentEditable工作,并且遇到头部融化问题。当我按下回车键时,Chrome会在innerHTML中插入一个新的div。这很好,很棒。问题是换行符无法在div的textContent中找到。我真的需要找出一种方法在与innerHTML中div分隔符相同的位置添加换行符到textContent。



任何想法?

更新:

我可以使用innerText,但是当页面加载被忽略时,会出现换行符。我需要通过其中一种方法保持一致。换句话说,我需要textContent来显示新输入的换行符或innerText以显示页面加载时存在的换行符。



这是一个更新的演示: data-console =falsedata-babel =falsedata-hide =falsedata-lang =jsdata-hide =false ()函数checkit(){var c1 = document.getElementById('');} $ {code> function checkit(){var c1 = document.getElementById(' c1')alert(TEXTCONTENT:\ n+ c1.textContent +\\\
\\\
INNERTEXT:\\\
+ c1.innerText +\\\
\\\
INNERHTML:\\\
+ c1.innerHTML )}

div {padding:20px; border-bottom:1px solid #CCC;}

< div>< a href =#onclick =checkit()>检查它< / a>< / div>< div contentEditable =trueid =c1> b> div< / b>,按回车键,然后按< b>检查它;< / b>以上< / div>

解决方案

我已经通过为每种情况加载了一个不同的变量来解决这个问题:

在页面加载时,我使用 textContent 保持换行符完好无损。当用户开始输入时,我使用 innerText 来识别插入的分页符。一个简单的if语句就可以做到这一点!


This is a complicated one! Im working with contentEditable in Chrome and I'm experiencing a head melting problem. When I press the return key, Chrome inserts a new div into the innerHTML. This is fine and dandy. The problem is that the line break is nowhere to be found in the div's textContent. I really need to figure out a way to add the line break to the textContent in the same place as the div break in the innerHTML.

Any Ideas?

UPDATE:

I can use innerText but then line breaks that are there when the page loads are ignored. I need consistency across one of these methods. In other words, I need textContent to show newly inputted line breaks or innerText to show line breaks that existed on page load.

Here's an updated demo:

function checkit() {
  var c1 = document.getElementById('c1')
  alert("TEXTCONTENT:\n" + c1.textContent + "\n\nINNERTEXT:\n" + c1.innerText + "\n\nINNERHTML:\n" + c1.innerHTML)
}

div {padding: 20px; border-bottom: 1px solid #CCC;}

<div><a href="#" onclick="checkit()">check it</a></div>

<div contentEditable="true" id="c1">click inside this <b>div</b>,
press return and then press <b>check it</b> above</div>

解决方案

I've solved this by loading a different variable for each situation:

On page load, I use textContent which keeps line breaks intact. When the user starts typing, I use innerText which recognises inserted page breaks. A simple if statement will do the trick!

这篇关于将换行符仅添加到textContent或innerText - 在Chrome中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆