A JavaScript removes the blank instance code at the end of the string

  • 2020-03-30 04:00:14
  • OfStack

<!DOCTYPE html> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
<title> Every day a JavaScript The instance - Removes whitespace at the end of the string </title> 
<script> 
function white(){ 
var input = document.getElementById("inputid"); 
var lines = input.value.split("n"); 
var resultString = ""; 
for (var i = 0; i < lines.length; i++){ 
var string = lines[i].trim(); 
resultString += string + "-"; 
} 
alert(resultString); 
} 
</script> 
</head> 

<body> 
<textarea id="inputid" placeholder=" Please enter a multi-line string "></textarea> 
<a href="#" onClick="white()">clickMe</a> 
</body> 
</html>

Detection of low version browsers, downward compatibility:


if(typeof String.trim == "undefined") 
String.prototype.trim = function(){ 
return this.replace(/(^s*)|(s*$)/g,""); 
} 
}

Related articles: