Javascript 将div值排序为带“quot;”&引用;而不是&引用;正确地
我使用下面的脚本对div值进行排序,但是这种排序方式的问题是,如果我使用“,”而不是“.”,它只能正确地处理数字-有没有办法处理点Javascript 将div值排序为带“quot;”&引用;而不是&引用;正确地,javascript,jquery,Javascript,Jquery,我使用下面的脚本对div值进行排序,但是这种排序方式的问题是,如果我使用“,”而不是“.”,它只能正确地处理数字-有没有办法处理点 var $divs = $("div.box"); $('#alphBnt').on('click', function () { var alphabeticallyOrderedDivs = $divs.sort(function (a, b) { return $(a).find("h1").text() > $(b).find
var $divs = $("div.box");
$('#alphBnt').on('click', function () {
var alphabeticallyOrderedDivs = $divs.sort(function (a, b) {
return $(a).find("h1").text() > $(b).find("h1").text();
});
$("#container").html(alphabeticallyOrderedDivs);
});
$('#numBnt').on('click', function () {
var numericallyOrderedDivs = $divs.sort(function (a, b) {
return $(a).find("h2").text() > $(b).find("h2").text();
});
$("#container").html(numericallyOrderedDivs);
});
在我的小提琴上,你可以看到,如果你点击“#numBnt”,日期没有正确排序
使用parseFloat($(a).find(“h2”).text().replace(“,”,”)代码>用于数字
至于日期
(新日期($(a).find(“h2”).text()).getTime()
您需要将带有日期的文本转换为JSDate
对象,如下所示:
新日期($(a).find(“h2”).text())
JS:
试着用不同的日期来测试小提琴
也过去
编辑:
Firefox修复程序:
Firefox不喜欢
作为日期的限定符。它需要替换为/
新日期($(a).find(“h2”).text().replace(/\./g,“/”)
您需要将日期值转换为dates对象以进行数字排序。首先,了解sort
的工作原理。您应该返回一个数字,而不是布尔值。一旦你把它排序了,就考虑<代码> .Read(/(\d)[,](\d)/g,'$ 1 $ 2)< /C> >或类似于删除数字之间的标点符号。根据操作方式,只需/(\d)、(\d)/
或/(\d)\(\d)/
即可。甚至parseFloat
。@Niet the Dark Absol:检查JS小提琴。这些值是dates@TrueBlueAussie老实说,我没有注意到小提琴,因为我在视觉上把它与标签或XD之类的东西混淆了,但除此之外,一个问题应该是自给自足的,而不是依赖外部小提琴来实际看到发生了什么。,
??我错过了什么吗?是不是只有一次如果我点击#alphBnt,然后再点击#numBnt,它就不起作用了?@Pepe它对我来说非常有效。可能是你的缓存?在chrome中,数字是错误的,在IE和Firefox上,它不能正常工作(按钮失效);(.@Pepe请检查我上面的编辑。在Firefox中测试,但在IE上没有。我没有IE.Thx作为您的支持和更新-就是这样,工作正常!大Thx!
var $divs = $("div.box");
$('#alphBnt').on('click', function () {
var alphabeticallyOrderedDivs = $divs.sort(function (a, b) {
return $(a).find("h1").text() > $(b).find("h1").text();
});
$("#container").html(alphabeticallyOrderedDivs);
});
$('#numBnt').on('click', function () {
var numericallyOrderedDivs = $divs.sort(function (a, b) {
// here is the change:
return new Date($(a).find("h2").text()) - new Date($(b).find("h2").text());
});
$("#container").html(numericallyOrderedDivs);
});