-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
cnyet
committed
Mar 1, 2019
1 parent
34d1577
commit e5a9dc9
Showing
10 changed files
with
217 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
body { | ||
margin: 0; | ||
padding: 0 | ||
} | ||
|
||
.arrange { | ||
padding: 0 2%; | ||
list-style-type: none | ||
} | ||
|
||
.arrange li { | ||
border: 1px solid #dfdfdf; | ||
height: 2em; | ||
line-height: 2em; | ||
padding-left: 2% | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
|
||
<head> | ||
<title>排序</title> | ||
<meta charset="UTF-8"> | ||
</meta> | ||
<meta author="alex"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1" /> | ||
</head> | ||
|
||
<body> | ||
<ul class="arrange"> | ||
<li class="arrangeLi">你好</li> | ||
<li class="arrangeLi">我是</li> | ||
<li class="arrangeLi">安妮</li> | ||
<li class="arrangeLi">可以</li> | ||
<li class="arrangeLi">做个</li> | ||
<li class="arrangeLi">一友</li> | ||
</ul> | ||
<script src="../../js/jquery.min.js" type="text/javascript"></script> | ||
<script src="sortUnicode.js" type="text/javascript"></script> | ||
<script src="sort.js" type="text/javascript"></script> | ||
<script> | ||
$(".arrange").sort(); | ||
</script> | ||
</body> | ||
|
||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
;(function($) { | ||
var Arrange = function(ele, options) { | ||
var list = ele.find("li.arrangeLi"); //获取容器下的li集合 | ||
|
||
this.dft = { //缺省值 | ||
"titleFontColor": "#fff", | ||
"titleBgColor": "#adadad", | ||
"conFontColor": "#000", | ||
"conBgColor": "#fff", | ||
"conBgColorHover": "#c2c2c2" | ||
}; | ||
this.obj = $.extend("", this.dft, options); | ||
this.obj.str = this.getStr(list); | ||
|
||
//判断首字母数组 | ||
var pinYin = this.executive(this.obj.str); // 得到一串首字母数组 | ||
var arry = []; | ||
for (var i = 0; i < pinYin.length; i++) { | ||
arry[i] = pinYin[i]; | ||
} | ||
var temp; | ||
var arrangeLi = ""; | ||
for (var i = 0; i < pinYin.length - 1; i++) { //将出现的字符按ascii顺序由小到大存进数组 | ||
for (var j = 0; j < pinYin.length - 1 - i; j++) { | ||
if (pinYin[j] > pinYin[j + 1]) { | ||
temp = pinYin[j]; | ||
pinYin[j] = pinYin[j + 1]; | ||
pinYin[j + 1] = temp; | ||
} | ||
} | ||
}; | ||
for (var i = 0; i < pinYin.length; i++) { //生成拥有的字符 | ||
if (pinYin[i] != pinYin[i - 1]) { | ||
arrangeLi += "<li class='arrangeTitle " + pinYin[i] + "'><b>" + pinYin[i] + "</b></li>"; | ||
} | ||
}; | ||
ele.html(arrangeLi); | ||
for (var i = 0; i < this.obj.str.length; i++) { //将各字段放进相应的字符区间 | ||
var li = "<li class='arrangeCon'>" + this.obj.str[i] + "</li>"; | ||
ele.find("." + arry[i] + "").after(li); | ||
}; | ||
|
||
this.setCss(this.obj); | ||
} | ||
Arrange.prototype = { | ||
setCss: function(obj) { //设置样式 | ||
$(".arrangeTitle").css({ | ||
"color": obj.titleFontColor, | ||
"backgroundColor": obj.titleBgColor | ||
}); | ||
$(".arrangeCon").css({ | ||
"color": obj.conFontColor, | ||
"backgroundColor": obj.conBgColor | ||
}); | ||
$(".arrangeCon").hover( | ||
function() { | ||
$(this).css({ | ||
"backgroundColor": obj.conBgColorHover | ||
}); | ||
}, | ||
function() { | ||
$(this).css({ | ||
"backgroundColor": obj.conBgColor | ||
}); | ||
} | ||
); | ||
}, | ||
executive: function(str) { //得到一串首字母数组 | ||
var isEnglishOrNum = /^[A-Za-z0-9]+$/; //正则验算是否英文字母或数字 | ||
var arr = []; //存储各字符串首字母 | ||
var j = 0; | ||
for (var i = 0; i < str.length; i++) { | ||
if (Object.prototype.toString.call(str[i]) === "[object String]") { //判断是否为字符串 | ||
var sing = str[i].substring(0, 1); //截取字符串首个字符 | ||
var ch = str[i].charCodeAt(0); //获取字符对应Unicode编码值 | ||
if (isEnglishOrNum.test(sing)) { //判断是否为英文字母或数字 | ||
arr[j++] = sing.toUpperCase(); //是则将字符赋值给数组 | ||
} else { | ||
|
||
if (ch < 40869 && ch >= 19968) { //判断字符编码值在19968到40869之间 | ||
arr[j++] = strChineseFirstPY.charAt(ch - 19968); //获取对应表中的字母并赋值到数组 | ||
} else { | ||
alert("字符串首字母仅支持字母和数字"); | ||
return false; | ||
} | ||
} | ||
} else { | ||
alert("请检查您输入的字符串数组是否有误"); | ||
return false; | ||
} | ||
} | ||
return arr; | ||
}, | ||
getStr: function(obj) { //获取当前对象下数组串 | ||
var str = []; | ||
obj.each(function() { | ||
str.push($(this).text()); | ||
}) | ||
return str; | ||
} | ||
|
||
} | ||
$.fn.sort = function(options) { | ||
this.each(function() { | ||
var arr = new Arrange($(this), options); | ||
}) | ||
|
||
} | ||
})(jQuery); |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.