Created
July 16, 2014 15:33
-
-
Save jikeytang/1f1cf9806f8ceb98a8c6 to your computer and use it in GitHub Desktop.
[ Javascript ] - 20140717-题目1
This file contains hidden or 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
有这样一个需求,页面上有连续的li若干个,然后不间断的用ul去包裹,如何实现。 | |
比如,有50个li,1-5个包一个ul,6-12包一个ul,13-30个包一个,31-50包一个。 | |
PS: | |
1. 回复时注意加上下面这句话,才会有语法高亮或格式缩进。 | |
```javascript | |
// you code | |
``` | |
2. 粘贴代码时请使用shift+tab,缩进前面的空白。 |
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>[ Javascript ] - 20140717-题目1</title>
<style type="text/css">
ul { background: #f00;}
ul li {list-style: none; }
ul li {background: #ddd; }
ul li:nth-child(even) {background: #efefef;}
</style>
</head>
<body style="background:white; font-size:18px">
<div id="div"></div>
<script type="text/javascript">
(function () {
var div = document.getElementById('div'),
strs = '',
listsB = [[0, 2], [3, 6], [7, 19], [20, 29], [30, 49]];
for (var i = 0, j = 0; i < 50; i++) {
strs += "<li>" + i + "</li>";
if (i == listsB[j][1]) {
listsB[j++] = "<ul>" + strs + "</ul>";
strs = '';
}
}
div.innerHTML = listsB.join('');
}());
</script>
</body>
</html>
<html>
<head>
<meta charset="utf-8">
<title>[ Javascript ] - 20140717-题目1</title>
<style type="text/css">
ul { background: #f00;}
ul li {list-style: none; }
ul li {background: #ddd; }
ul li:nth-child(even) {background: #efefef;}
</style>
</head>
<body style="background:white; font-size:18px">
<div id="div"></div>
<script type="text/javascript">
(function(l,r){
var re = [],index = 1,i=0,html = '<ul>',n;
new Array(l+1).join('s').replace(/s/g,function(){ re.push('<li>'+(index++)+'</li>')});
while(i < r.length){
n = r[i]
r[i++] = re.slice(n[0],n[1]+1).join('');
};
html += r.join('</ul><ul>') + '</ul>';
document.getElementById('div').innerHTML = html;
})(50,[[0, 2], [3, 6], [7, 19], [20, 29], [30, 50]]);
</script>
</body>
</html>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
demo jsbin