Home >Web Front-end >JS Tutorial >jQuery method to replace line breaks in textarea_jquery

jQuery method to replace line breaks in textarea_jquery

WBOY
WBOYOriginal
2016-05-16 15:55:561411browse

The example in this article describes how jQuery replaces line breaks in textarea. Share it with everyone for your reference. The specific analysis is as follows:

When my colleague was learning jQuery yesterday, he wanted to replace the line breaks in the textarea.

html part:

<fieldset>
 <textarea id="ncontent"></textarea>
</fieldset>
<button id="submit">提  交</button> 

js part:

$(document).ready(function(){ 
  $("#submit").click(function(){ 
    var ss = $("#ncontent").text(); 
    alert(ss); 
    var str = ss.replace(//r/n/gi, "!!!"); 
    alert(str); 
  }); 
}); 
</script>

After using JavaScript’s replace() method, the newline is still not replaced.

The problem is that jQuery gets the value in textarea. Should use

Copy code The code is as follows:
var ss = $("#ncontent").html();

Using the text() or val() method will process the html tags in the textarea, so /r/n will definitely not be replaced.

I hope this article will be helpful to everyone’s jQuery programming.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn