jquery 替换空格在 IE 中不起作用

发布于 2024-12-06 01:34:12 字数 761 浏览 0 评论 0原文

我对 jquery 有点陌生,所以我希望有人可以帮助我解决这个问题。

我正在研究 grails,并且 我有这个jQuery对象:

var tableIds = $("#myTable tbody tr td:first-child").text().toString()

当我alert()这个时,它有 id的 myTable上每一行的一些信息(第一列),例如:

1 1213 2324 23123

(以获取id之间的空格)必须在 myTable 的第一个 td 中手动添加 '& nbsp;')

现在,当我尝试:

var idArray = new Array()
idArray = tableIds.split(" ")

它不起作用时,“split”只留下idArray 作为字符串,原始空格

与 tableIds 一样。

我要做的就是用连字符替换空格:

tableIds = tableIds.replace(/\s/g,"-")

然后 split("-") 起作用,不知道为什么......但仅限于 Chrome!不是在 IE 中,我需要这个才能在愚蠢的 IE 中工作。

IE 一直向我显示“1 1213 2324 23123”,它没有找到要替换的空格,就这样保留了它。

有人对此有任何线索吗? 希望你能帮助我,如果没有的话,还是谢谢你。

I'm sort of new to jquery so I hope somebody can help me with this problem.

I'm working on grails, and
I have this jQuery object:

var tableIds = $("#myTable tbody tr td:first-child").text().toString()

when I alert() this, it has the id's some info (first column) of every row on myTable like:

1 1213 2324 23123

(to get the spaces between the id's I had to add manually the '& nbsp;' in the first td of myTable)

Now, when I try to:

var idArray = new Array()
idArray = tableIds.split(" ")

it doesn't work, the "split" just leave the idArray as a string with the original spaces

just as tableIds.

What I had to do was to replace the spaces with hyphens:

tableIds = tableIds.replace(/\s/g,"-")

and then split("-") works, dunno why... but ONLY in CHROME!! not in IE, and I need this to work in the stupid IE.

IE keeps showing me "1 1213 2324 23123", it did not found the spaces to replace and just left it like that.

Anyone have a clue on this?
Hope you can help me, if not, thanks anyway.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

绮烟 2024-12-13 01:34:12

我认为您可能以错误的方式处理这件事。如果您想获取 jQuery 通过该选择器拾取的元素的 ID 数组,请尝试这样做:

var ids = [];
$("#myTable tbody tr td:first-child").each(function () {
    ids.push($(this).attr('id'));
});
//now the ids array will have all of the ID's of the elements in it

“each”对所选的每个元素运行传递的函数..这样您就可以获取所有 ID非常可靠。可能还有更优化的方法,但这个方法从来没有让我失望过。

编辑:重新阅读后,我意识到你可能正在寻找你给出的文本ID..类似于..

| 1 | dog    | $5
| 2 | cat    | $2
| 3 | fish   | $1.10

所以..使用与我上面写的相同的想法...

var ids = [];
$("#myTable tbody tr td:first-child").each(function () {
    ids.push($(this).text());
});
//ids will be [1, 2, 3]

I think you might be going about this in the wrong way. If you want to get an array of ID's of the elements that jQuery has picked up via that selector, try doing this instead:

var ids = [];
$("#myTable tbody tr td:first-child").each(function () {
    ids.push($(this).attr('id'));
});
//now the ids array will have all of the ID's of the elements in it

The 'each' runs the passed function over every element that was selected.. so you can grab all of the ID's like that very reliably. There might be more optimized ways, but this method has never failed me.

Edit: after re-reading, I realized you're probably looking for the text id that you've given.. something like..

| 1 | dog    | $5
| 2 | cat    | $2
| 3 | fish   | $1.10

So.. using the same idea as I wrote above...

var ids = [];
$("#myTable tbody tr td:first-child").each(function () {
    ids.push($(this).text());
});
//ids will be [1, 2, 3]
水晶透心 2024-12-13 01:34:12

尝试将每个 id 后面的   空格替换为常规空格。

Try replacing your   spaces with regular white space after each id.

南巷近海 2024-12-13 01:34:12

嗯......我可能会假设你的错误,但请耐心等待:

听起来你正在将你想要选择的每个 td 的 ID 设置为带有 a 的字符串其中有一堆空格(或连字符,或其他什么)。

jQuery 的选择器语法是内置的,因此您不必这样做。您应该能够使用上述语法选择所需的元素,然后使用each() 循环迭代它们。考虑一下:

var anArray = [];

$("#myTable tbody tr td:first-child").each(function(){
    //This code will run for every element matched by the above selector
    var contentsOfCell = $(this).text();
    anArray.push(contentsOfCell);
});

希望这有帮助!

Hmmm... I could be way off in assuming your mistake, but bear with me here:

It sounds like you're setting the ID of each td that you want to select to be a string with a bunch of spaces in it (or hyphens, or whatever).

jQuery's selector syntax is build so that you don't have to do this. You should be able to select the elements you want with the above syntax, and then iterate over them with an each() loop. Consider this:

var anArray = [];

$("#myTable tbody tr td:first-child").each(function(){
    //This code will run for every element matched by the above selector
    var contentsOfCell = $(this).text();
    anArray.push(contentsOfCell);
});

Hope this helps!

奢望 2024-12-13 01:34:12

您可以使用 jQuery 的 .each() 提取每行第一个 的内容,以迭代初始选择器中的所有结果,将每个值放入数组:

var idArray = [];

$("#myTable tbody tr td:first-child").each(function() {
   idArray.push($(this).text());
});

我不知道正则表达式找不到空格是怎么回事,除非 IE 认为不间断空格不是空格?

You can extract the contents of the first <td> of each row using jQuery's .each() to iterate over all the results from your initial selector, putting each value in the array as you go:

var idArray = [];

$("#myTable tbody tr td:first-child").each(function() {
   idArray.push($(this).text());
});

I don't know what's up with the regex not finding the spaces, unless IE thinks that non-breaking spaces are not whitespace?

忘东忘西忘不掉你 2024-12-13 01:34:12

谢谢大家!我没有尝试用简单空格替换不间断空格,因为在我的 TD 上手动添加空格实际上并不是一个好主意。

但正如 Stephen、nnnnnn 和 Chazbot 所说,这成功了!

var ids = [];
$("#myTable tbody tr td:first-child").each(function () {
    ids.push($(this).text());
})

我不知道你可以“每个”这种 jquery 对象,我应该知道它:D
有新东西要学。

再次感谢!

Thanks to everyone! I didn't try replacing non-breaking spaces with simple spaces, becouse adding spaces manually on my TD's wasn't actually a good idea.

but as Stephen and nnnnnn and Chazbot said, this did the trick!

var ids = [];
$("#myTable tbody tr td:first-child").each(function () {
    ids.push($(this).text());
})

I didn't know you can "each" this kind of jquery objects, I should've known it :D
Something new to learn.

Thanks again!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文