如何使用 KineticJS 在 javascript 变量中添加换行符?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16595483/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 05:16:25  来源:igfitidea点击:

How do you add a line break inside of a javascript variable using KineticJS?

javascriptkineticjs

提问by user761479

I have a variable that looks like this:

我有一个看起来像这样的变量:

var simpleText = new Kinetic.Text({
    text: 'where text goes',
    fontSize: 24
  });

How do I insert a line break where the text goes. I've tried:

如何在文本所在的位置插入换行符。我试过了:

text: 'where text goes \n where text goes',

text: 'where text goes \n\n where text goes',

text: 'where text goes \r\n where text goes',

text: 'where text goes <br /> where text goes',

text: 'where text goes \n' + 'where text goes \n',

text: 'where text goes ' + 'where text goes ',

text: 'where text goes ' 
+ 'where text goes ',

text: 'where text goes 
where text goes',

Error on my end. \n works

我的错误。\n 有效

回答by Niet the Dark Absol

If you are outputting to HTML, such as with innerHTML, just use <br />.

如果您要输出到 HTML,例如 with innerHTML,只需使用<br />.

If, however, you are outputting to text in an HTML context (such as nodeValueor innerText, you will have to use \nand ensure that the container has the CSS white-spaceproperty set to something that doesn't condence whitespace, such as pre-wrap.

但是,如果您要输出到 HTML 上下文中的文本(例如nodeValueinnerText,则必须使用\n并确保容器将 CSSwhite-space属性设置为不包含空格的内容,例如pre-wrap.

回答by MBillau

I've never heard of Kinetic text but based on this sample: http://www.html5canvastutorials.com/kineticjs/html5-canvas-kineticjs-text-tutorial/you should do:

我从未听说过 Kinetic 文本,但基于此示例:http: //www.html5canvastutorials.com/kineticjs/html5-canvas-kineticjs-text-tutorial/你应该这样做:

text: 'where text goes\n\nwhere text goes',

text: 'where text goes\n\nwhere text goes',

回答by Keith Male

Using html() instead of 'text' and 'val'

使用 html() 而不是 'text' 和 'val'

.html('using some -br- simple text here');

.html('在此处使用一些 -br- 简单文本');

Result: using some
simple text here

结果:
在这里使用一些简单的文本