简体   繁体   中英

Break line using javascript

I want to break my string using javascript which is fetching some data from a php file using ajax. The php is returning the correct data, however the javascript doesn't treat a <br> tag properly. This is my ajax code and i am using it to replace the current text in that span .

type: "post", url: "some_php.php", data: "name="+name,
    success: function(data) {
      $('#someDiv').text(data);
    }

Using console.log(data) i get the output as:

Some<br>Address<br>to<br>be<br>borken

No i want it to output as

Some
Address
to
be
broken

使用.html()

 $('#someDiv').html(data);

You're using the wrong jQuery function:

$('#someDiv').html(data);

From jQuery's text() function :

.text( textString ) Set the content of each element in the set of matched elements to the specified text.

From jQuery's html() function :

.html( htmlString ) Set the HTML contents of each element in the set of matched elements.

console.log(data.split('<br>').join('\n'));

You can use the previous method to replace all <br> s with newlines.

If you want to have one console entry for each line, use:

console.log.apply(console, data.split('<br>'));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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