jQuery修剪变量中的所有空格 [英] Jquery Trim all spaces from variable

查看:123
本文介绍了jQuery修剪变量中的所有空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

单击后,我将存储一个变量.然后,我想为所有空白修剪该变量.我只是在console.log新变量时遇到错误.

On a click I store a variable. I would like to then trim that variable for all whitespace. I am getting an error when I to simply console.log the new variable.

var songToTrim = $(this).html();
console.log(songToTrim);
var songToPlay = songToTrim.replace(/ /g,'');
console.log(songToPlay);

对于奖励积分,如果您可以添加如何确保将变量转换为所有大写小写字母的方法.如果没有,它仍然可以工作.谢谢!

For bonus points if you can add how to make sure the variable is converted to all lowercase that would be huge. If not it'll still work. Thanks!

推荐答案

这是我的解决方法

代码:

 $("#test").on('click', function(){
    var songToTrim = $(this).html();
   var trm  = songToTrim.replace(/ /g,'');
   var songToPlay = trm.toLowerCase();
    });

使用.toLowerCase()将变量转换为小写.

Use .toLowerCase() to convert your variable to Lower Case.

您得到的错误是因为您的代码中某处有一个 typo .您将Console拼写为Consle.

The error you are getting is because you have a typo in your code somewhere. You have misspelled Console as Consle.

找到并更正.

这是工作正常的 演示

Here is a working Demo

这篇关于jQuery修剪变量中的所有空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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