Jquery教程
Jquery动效
Jquery HTML/CSS
Jquery 事件
jquery常用

Jquery trim()方法

trim()方法用于删除开头和结尾的空格,制表符和所有换行符指定的字符串。如果这些空格字符位于字符串的中间,则此方法不会删除这些字符。
使用此方法的常用语法如下。

语法

jQuery.trim( str )
trim()方法接受单个参数 str ,其定义如下。
str:

示例

在此示例中,首先,我们必须单击给定的按钮。单击按钮后,将显示一个警告框,显示原始字符串。关闭相应的警报框后,将显示另一个警报框,其中显示了修剪后的字符串。
此处,原始字符串在开头和结尾处都包含多个新行,制表符和空格的字符串。
<!DOCTYPE html>
<html>
<head>
<title> jQuery trim() method</title>
<script src = "/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>
<h4> This is an example of using trim() method </h4>
<p style = "color: blue;"> Click the following button to trim the string </p>
<button id = "btn"> Click me </button>
<script>
$(document).ready(function(){
$("#btn").click(function(){
var str = "\n\n Welcome to the lidihuo.com \n\n";
alert("The original string is: " + str);
str = jQuery.trim(str);
alert("The trimmed string is: " + str);
});
});
</script>
</body>
</html>
输出:

This is an example of using trim() method

Click the following button to trim the string

昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4