最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - JavaScript: How to add 'word-wrap' style to element - Stack Overflow

programmeradmin1浏览0评论

How do you add the 'word-wrap' style to an element using 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.

How do you add the 'word-wrap' style to an element using 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.

Share Improve this question edited Aug 3, 2015 at 17:03 baao 73.4k18 gold badges150 silver badges207 bronze badges asked Aug 2, 2015 at 14:23 ExbiExbi 9704 gold badges12 silver badges19 bronze badges 1
  • Can you make a fiddle – EugenSunic Commented Aug 2, 2015 at 14:25
Add a ment  | 

2 Answers 2

Reset to default 5

Just use wordWrap instead 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. - is the arithmetic subtraction operator, it can't used in identifiers.

You could use bracket notation instead but it's not remended

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

Javascript uses camel case for style property declarations

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

wordWrap is a object key, object keys must not include -.

发布评论

评论列表(0)

  1. 暂无评论