简体   繁体   English

JavaScript:如何向元素添加“自动换行”样式

[英]JavaScript: How to add 'word-wrap' style to element

How do you add the 'word-wrap' style to an element using JavaScript? 如何使用JavaScript将“自动换行”样式添加到元素?

tables = document.getElementsByTagName("TABLE");
for (i = 0; i < tables.length; i++)
    tables[i].style.word-wrap = 'break-word';

The above code produces the following error: 上面的代码产生以下错误:

ReferenceError: invalid assignment left-hand side

Although the code works if I used a different style, ie... 虽然如果我使用其他样式,代码也可以工作,即...

tables[i].style.width = '300px';

'word-wrap' seems to be what I'm looking for though, as it works within a test CSS file and within HTML style tags. 我似乎正在寻找“自动换行”,因为它可以在测试CSS文件和HTML样式标签中使用。

Just use wordWrap instead word-wrap 只需使用wordWrap代替word-wrap

tables = document.getElementsByTagName("TABLE");
for (i = 0; i < tables.length; i++)
    tables[i].style.wordWrap = 'break-word';

Style names in Javascript use camelCase. Javascript中的样式名称使用​​camelCase。 - is the arithmetic subtraction operator, it can't used in identifiers. -是算术减法运算符,不能在标识符中使用。

You could use bracket notation instead but it's not recommended 您可以改用括号表示法,但不建议使用

tables[i].style['word-wrap'] = 'break-word';

Javascript uses camel case for style property declarations Javascript使用驼峰大小写进行样式属性声明

tables[i].style.wordWrap = 'break-word';

wordWrap is a object key, object keys must not include - . wordWrap是对象键,对象键不能包含-

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM