0

My div of "description" class pulls in the following from an array:

[{ "id": "1", "text": "There are two.\n\nThe first one is..."}]

How can I ensure instead of it just creating the space it replaces the \n\n with a <br /><br />?

I've tried

document.getElementById(".description").innerHTML = myObj.text.replace(/\n/g,"<br /><br />");

but no luck.

4

1 回答 1

0

It almost looks like you may be trying to use a selector:

document.getElementById(".description").innerHTML = ...;
// TypeError: Cannot set property 'innerHTML' of null

But, getElementById() expects just id="..." values. So, it'll be trying to find:

<div id=".description"></div>

If description is a class name:

<div class="description"></div>

You could try document.querySelector() instead (IE8+).

document.querySelector('.description').innerHTML = myObj.text.replace(/\n/g,"<br /><br />");

Example: http://jsfiddle.net/4xUwn/

于 2013-08-25T09:37:24.600 回答