http://jsfiddle.net/fkling/nXkDp/js按钮上的id排序
嗨,即时尝试将此脚本连接到按钮,但它不工作。我不知道为什么。
var sortID = function()
{
var toSort = document.getElementById('list').children;
toSort = Array.prototype.slice.call(toSort, 0);
toSort.sort(function(a, b) {
var aord = +a.id.split('-')[1];
var bord = +b.id.split('-')[1];
// two elements never have the same ID hence this is sufficient:
return (aord > bord) ? 1 : -1;
});
var parent = document.getElementById('list');
parent.innerHTML = "";
for(var i = 0, l = toSort.length; i < l; i++) {
parent.appendChild(toSort[i]);}
};
按钮在哪里? – mishik
可能是因为您的HTML缺少按钮 – SomeShinyObject
问题“如何将此代码挂接到按钮?”或者“为什么这种排序功能不适用于我的清单?”编辑:我只是看着小提琴,显然这种工作。您是否告诉我们您可以编写这样的排序函数,但是您不能执行按钮单击事件处理程序? – nnnnnn