1.统计字数
<div class="value">
<textarea name="content" class="textarea" id="textarea" cols="30" rows="10" size="10" οnkeyup="figure()" maxlength="500"></textarea>
<div class="error">您还可以输入<span id="sp">500</span>字</div>
</div>
//输入字数计算
function figure() {
var textarea = document.getElementById("textarea");
var maxlength = 500;
var length = textarea.value.length;
var count = maxlength - length;
var sp = document.getElementById("sp");
sp.innerHTML = count;
if (count <= 10) {
sp.style.color = "red";
} else {
sp.removeAttribute("style");
}
}
2.返回顶部
①:
const scrollToTop = _ => {
const c = document.documentElement.scrollTop || document.body.scrollTop;
if (c > 0) {
window.requestAnimationFrame(scrollToTop);
window.scrollTo(0, c - c / 8);
}
};
scrollToTop();//注:window.requestAnimationFrame()方法有兼容问题
②:
showScroll();
function showScroll(){
$(window).scroll( function() {
var scrollValue=$(window).scrollTop();
scrollValue > 100 ? $('div[class=scroll]').fadeIn():$('div[class=scroll]').fadeOut();
} );
$('#scroll').click(function(){
$("html,body").animate({scrollTop:0},200);
});
}
3.寻找几种数组去重
const unique = arr => [...new Set(arr)];
// unique([1,2,2,3,4,4,5]) -> [1,2,3,4,5]
// 1. 双层循环,外层循环元素,内层循环时比较值,
// 如果有相同的值则跳过,不相同则push进数组
Array.prototype.distinct = function(){
var arr = this,
result = [],
i,
j,
len = arr.length;
for(i = 0; i < len; i++){
for(j = i + 1; j < len; j++){
if(arr[i] === arr[j]){
j = ++i;
}
}
result.push(arr[i]);
}
return result;
}
var arra = [1,2,2,4,4];
console.log(arra.distinct());//1,2,4
// 2. 利用splice直接在原数组进行操作
/*双层循环,外层循环元素,内层循环时比较值,值相同时,则删去这个值
注意点:删除元素之后,需要将数组的长度也减1.*/
Array.prototype.distinct = function (){
var arr = this,
i,
j,
len = arr.length;
for(i = 0; i < len; i++){
for(j = i + 1; j < len; j++){
if(arr[i] == arr[j]){
arr.splice(j,1);
len--;
j--;
}
}
}
return arr;
};
var a = [1,2,2,1,1,];
var b = a.distinct();
console.log(b.toString()); //1,2
4.
zepto和jQuery中的select option选中的值
zepto option:checked
jquery option:selected