如何在普通的Javascript文件中使用JQuery trim()函数? [英] How to use the JQuery trim() function into normal Javascript file?

查看:41
本文介绍了如何在普通的Javascript文件中使用JQuery trim()函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好

无论最终用户将在文本框中键入什么内容,我都将使用以下代码在客户端的字符串中修剪多余的空间

if(document.getElementById("txtUserName").value.trim() == ""){
       ..........
       ..........
}



当此行由clinet端的浏览器执行时

它显示错误对象不支持此属性或方法错误"

如何在javascript中使用trim()函数

请帮助我

在此先感谢...

解决方案

 <   html  > 
<  头部 > 
  <  脚本    ="   http://code.jquery.com/jquery-1.5. js" <  > 
<  /head  > 
<  正文 > 
  <  按钮 > 显示修剪示例<  /button  > 
<  脚本 >  

("button").click(function(){ var str =前后有很多空格"; alert('" + str +'"); str = jQuery.trim(str); alert('" + str +'-不再"); }); < /script > < /body > < /html >


有几种方法可以使用字符串对象 [ ^ ].

声明一个字符串,为其分配文本框值,然后使用字符串方法来实现修整功能.如果需要,将字符串值分配回文本框.

下面是一个示例: JavaScript字符串修剪 [.trim() == ""){ .......... .......... }



when this line execute by the browser on clinet side

it shows the error "Object doesn''t support this property or method Error"

How to use the trim() function in javascript

Please Help me

Thanks in Advance...

解决方案

<html>
<head>
  <script src="http://code.jquery.com/jquery-1.5.js"></script>
</head>
<body>
  <button>Show Trim Example</button>
<script>


("button").click(function () { var str = " lots of spaces before and after "; alert("'" + str + "'"); str = jQuery.trim(str); alert("'" + str + "' - no longer"); }); </script> </body> </html>


There are few methods to achieve trim using string object[^].

Declare a string, assign the textbox value to it and then use string methods to achieve trim feature. If needed, assign back the string value to textbox.

Here an example: Javascript string Trim[^]


这篇关于如何在普通的Javascript文件中使用JQuery trim()函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆