Trim to remove white space

ぐ巨炮叔叔 提交于 2019-11-28 18:17:16

问题


jQuery trim not working. I wrote the following command to remove white space. Whats wrong in it?

var str = $('input').val();

str = jquery.trim(str);
console.log(str);

Fiddle example.


回答1:


jQuery.trim() capital Q?

or $.trim()




回答2:


No need for jQuery

JavaScript does have a native .trim() method.

var name = "    John Smith  ";
name = name.trim();

console.log(name); // "John Smith"

Example Here

String.prototype.trim()

The trim() method removes whitespace from both ends of a string. Whitespace in this context is all the whitespace characters (space, tab, no-break space, etc.) and all the line terminator characters (LF, CR, etc.).




回答3:


or just use $.trim(str)




回答4:


Why not try this?

html:

<p>
  a b c
</p>

js:

$("p").text().trim();


来源:https://stackoverflow.com/questions/4040259/trim-to-remove-white-space

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!