0

我想对从 attr 获取的变量使用 split 方法。

这是我正在尝试的代码:

$(document).ready(function() {
    $('.some_divs').each(function() {
       var id = $(this).attr('id');
       var ida = id.spilt("_");
       alert(ida[1]);
    });
});

但是,当我运行我的代码时,它会抛出一个错误:Object id_1 has no method 'spilt'。

我尝试使用.toStringString()将变量转换为字符串。

可能我在这里遗漏了一些基本的东西。那会是什么?

4

3 回答 3

4

应该split不是spilt

var ida = id.split("_");

在不同的说明中,您可以只使用this.id而不是$(this).attr('id').

$(document).ready(function() {
    $('.some_divs').each(function() {
       var id = this.id;
       var ida = id.split("_");
       alert(ida[1]);
    });
});
于 2013-05-22T14:33:14.453 回答
2

做就是了

String.prototype.spilt = String.prototype.split

:D

于 2013-05-22T14:35:50.120 回答
1

你有一个语法错误,使用split而不是spilled

于 2013-05-22T14:35:56.403 回答