javascript 用“ ”替换空格 使用jQuery

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3941611/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 02:46:37  来源:igfitidea点击:

Replace a empty space with " " using Jquery

javascriptjquerystring

提问by Alessio

I am looking around on the web but I am not finding anything useful. :(

我在网上环顾四周,但没有找到任何有用的东西。:(

I am having a problem that I don't understand. I am sure that I am doing something wrong, but I don't know well the syntax of jQuery to understand what is that I am not doing right.

我有一个我不明白的问题。我确信我做错了什么,但我不太了解 jQuery 的语法来理解我做错了什么。

I am using animations with JS and CSS 3, and I am having troubles with empty spaces between the words, and to solve this problems I have to find a way to substitute chars inside a string of text with something else. Like an empty space with a  , or as a test that I was trying to do a "n" with a "xxxxx".

我正在使用带有 JS 和 CSS 3 的动画,并且在单词之间出现空格的问题,为了解决这个问题,我必须找到一种方法来用其他内容替换文本字符串中的字符。就像一个带有 的空白空间,或者作为我试图用“ n”做一个“ ”的测试xxxxx

What I think that I am doing is:

我认为我正在做的是:

  1. when the page is loaded
  2. Modify the string of any paragraph with the class .fancy-titlethat contains "n" with a text "xxxxx"
  1. 页面加载时
  2. .fancy-title包含“ n”的类修改任何段落的字符串,并带有文本“ xxxxx

So:

所以:

$(document).ready(function(){
    for(i=0; i< myLength+1; i++){
        var charCheck = $(".fancy-title").text()[i];
        if(charCheck == "n"){
            charCheck.replace("n", "xxxxxxxx");
        }
    }
});

But I receive an error that it said:

但我收到一个错误,它说:

charCheck.replace("n", "xxxxxxxx"); it is not a function

charCheck.replace("n", "xxxxxxxx"); 它不是一个函数

I am using jquery

我正在使用 jquery

and other scripts that are based on jquery to make animations, rotation and scaling... and they are all in the HEAD with jquery first to load.

和其他基于 jquery 制作动画、旋转和缩放的脚本……它们都在 HEAD 中,jquery 首先加载。

What am I doing wrong? Manipulation in jQuery does it need a specific .js extension? I understood that was in the basic capability of jQuery, and looking at other examples all creates me the same kind of error.

我究竟做错了什么?jQuery 中的操作是否需要特定的 .js 扩展名?我知道这是 jQuery 的基本功能,查看其他示例都会给我带来同样的错误。

I even tried

我什至试过

if(charCheck == "n"){
   $(".fancy-title").text()[i] == "&nbsp;"
}

But simply the modification it is not applied on the page. I tried with innerHTMLas well :( I feel so incompetent...

但只是简单的修改它并没有应用到页面上。我也试过innerHTML:(我觉得很无能......

Thank you in advance for any help.

预先感谢您的任何帮助。

回答by Ned Batchelder

$(document).ready(function(){
    $(".fancy-title").each(function(i){
        var text = $(this).html();
        $(this).html(text.replace(/ /g, "&nbsp;"));
    })
});

回答by kapa

You have no problem with the replace part :).

更换零件没有问题:)。

$(document).ready(function(){
    $(".fancy-title").each(function () { //for all the elements with class 'fancy-title'
         var s=$(this).text(); //get text
         $(this).text(s.replace(/n/g, 'xxxxxxxx')); //set text to the replaced version
    });
});

Just a quick example, hope it works.

只是一个简单的例子,希望它有效。

回答by sod

Have you tried the css style white-space:preinstead of replacing ' ' with '&nbsp;'? http://de.selfhtml.org/css/eigenschaften/ausrichtung.htm#white_space

您是否尝试过使用 css 样式white-space:pre而不是将 ' ' 替换为 ' '? http://de.selfhtml.org/css/eigenschaften/ausrichtung.htm#white_space

回答by Michael Banzon

It seems you are trying to replace a single character with a new string.

您似乎正在尝试用新字符串替换单个字符。

You might be able to get the right result by dropping the iteration and simply call .replace on the jQuery-object.

您可以通过删除迭代并简单地在 jQuery 对象上调用 .replace 来获得正确的结果。