Home >Web Front-end >JS Tutorial >How to implement jquery $.trim() to remove string spaces [with legend]_jquery
How to implement jquery $.trim() to remove string spaces [with legend]
Grammar
The jQuery.trim() function is used to remove whitespace characters at both ends of a string.
Function
This function can remove whitespace characters at both the beginning and end of a string (until the first non-whitespace string is encountered). It will remove common whitespace characters including newlines, spaces, tabs, etc.
Parameters
If the parameter str is not of string type, this function will automatically convert it to a string (usually calling its toString() method). If the parameter str is null or undefined, an empty string ("") is returned.
Return value
The return value of the jQuery.trim() function is of String type and returns the string after removing the blank strings at both ends.
Examples & Instructions
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <input type="text" name="" id="results" value=""/><br> <button id="showBtn">showBtn</button> <button id="showBtn1">showBtn1</button> <script src="http://code.jquery.com/jquery-1.5.js"></script> <script type="text/javascript"> $("#showBtn").click( function(){ var content = $('#content').val(); if($.trim(content) == ''){ alert('空'); } } ); $("#showBtn1").click( function(){ var content = $('#content').val(); if(content.trim() == ''){ alert('空'); } } ); </script> </body> </html>
We cannot take it for granted that it is the same as Java. Use a string dot method.
Wrong spelling:
$("#showBtn1").click( function(){ var content = $('#content').val(); if(content.trim() == ''){ alert('空'); } } );
Note: The above writing method will not report an error under Firefox, but will indeed report an error under IE and Google.
Correct writing:
$("#showBtn").click( function(){ var content = $('#content').val(); if($.trim(content) == ''){ alert('空'); } } );
The above implementation method of removing string spaces by jquery $.trim() [with legend] is all the content shared by the editor. I hope it can give you a reference, and I hope you will support Script Home.