javascript 子字符串javascript根本不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13434435/
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-26 18:45:41  来源:igfitidea点击:

substring javascript not working at all

javascript

提问by user1066524

is there any reason why this wouldn't be working?

有什么理由让这行不通吗?

     var caseString = "sliderInput";
     var theString = caseString.substring(1, 2);

I put it through the Firebug debugger in Firefox and it is giving me the error: "invalid assignment left-hand side."

我把它通过 Firefox 中的 Firebug 调试器,它给了我错误:“左侧分配无效。”

**here is my exact code

* *这是我的确切代码

        var elements = new Array();
        elements = document.getElementsByTagName("Input");


var allSliderInputs = new Array();
var sliderParams = new Array();
var first, last, inc, style;

for (var i=0; i < elements.length ; i++){
    var c = elements[i].className; //works fine here
    var t = c.substring(0, 2); //when it hits this line it says "invalid assignment left-hand side"

 }

回答by Joe

First, .substring (and .substr) is 0-based, not 1-based.

首先,.substring(和 .substr)是基于 0 的,而不是基于 1 的。

.substringextracts a string between two positions. E.g. .substring(1,4)returns the 2nd, 3rd, and 4th characters. It will stop at position 4.

.substring提取两个位置之间的字符串。例如,.substring(1,4)返回第 2、3 和 4 个字符。它将停在位置 4。

.substrextracts a string based on start + length. .substr(1,4)returns the first 4 characters starting with the 2nd character.

.substr根据开始 + 长度提取字符串。 .substr(1,4)返回从第二个字符开始的前 4 个字符。

回答by Minko Gechev

var theString = caseString.substring(1, 2);should return you just l. The substringmethod is accepting as first argument the start of the substring and as second argument the end of the substring. You're giving it index 1 which is the second character (0 is the first).

var theString = caseString.substring(1, 2);应该还给你吧l。该substring方法接受子串的开头作为第一个参数,子串的结尾作为第二个参数。你给它索引 1 这是第二个字符(0 是第一个)。

You probably have mistaken substringwith substr. substraccepts as first argument start index and length as second. So:

你可能误会substringsubstr. substr接受作为第一个参数开始索引和长度作为第二个。所以:

var caseString = "sliderInput";
var theString = caseString.substr(0, 2); //sl

is giving the result you want.

正在给出你想要的结果。

回答by Jamund Ferguson

substringis 0-indexed so you should instead do something like this:

substring是 0-indexed 所以你应该做这样的事情:

var word = "slider";
var part = word.substring(0,2); // sl

Also take note that .slice()does the same thing but is actually more powerful, because it can count backwards as well as forwards.

还要注意.slice()做同样的事情,但实际上更强大,因为它可以向后计数也可以向前计数。

To solve your new problem I would suggest a few things:

为了解决您的新问题,我建议几点:

  1. You need to cache your length value. The list returned by getElementsByTagNameis live meaning any changes to your list while you're looping will effect that value, so it won't behave as you'd expect.
  2. Don't use new Array()it's overly fancy.
  3. You don't need to instantiate variables that you're going to define right after.
  1. 您需要缓存您的长度值。返回的列表getElementsByTagName是实时的,这意味着在循环时对列表的任何更改都会影响该值,因此它不会像您期望的那样运行。
  2. 不要用new Array()它太花哨了。
  3. 您不需要实例化您将在之后立即定义的变量。

Try this:

试试这个:

var elements = document.getElementsByTagName("input");
var allSliderInputs = [];
var sliderParams = [];
var len = elements.length;
var first, last, inc, style, c, t, i;

for (i = 0; i < len; i++) {
    c = elements[i].className; //works fine here
    t = c.substring(0, 2); 
    console.log(t);
}

This works fine for me in Firefox when run on this very page in stackoverflow: sample result

当在 stackoverflow 中的这个页面上运行时,这对我在 Firefox 中很好用: 样本结果