javaScript总结20160809

来源:互联网 发布:西单附近美食知乎 编辑:程序博客网 时间:2024/03/29 18:50

1.得到本周的星期五

   

function getfriday(){<span style="white-space:pre"></span><span style="white-space:pre"></span>var time= new Date();<span style="white-space:pre"></span>var weekday=time.getDay();<span style="white-space:pre"></span> var off=5-weekday;<span style="white-space:pre"></span> time=time.getTime();<span style="white-space:pre"></span> var fri=time+off*24*3600*1000;<span style="white-space:pre"></span> fri= new Date(fri);<span style="white-space:pre"></span> return fri.format("yyyy-MM-dd");
}
Date.prototype.format = function(fmt) {<span style="white-space:pre"></span>var o = {<span style="white-space:pre"></span>'M+': this.getMonth() + 1,<span style="white-space:pre"></span>'d+': this.getDate(),<span style="white-space:pre"></span>'h+': this.getHours(),<span style="white-space:pre"></span>'m+': this.getMinutes(),<span style="white-space:pre"></span>'s+': this.getSeconds(),<span style="white-space:pre"></span>'q+': Math.floor((this.getMonth() + 3) / 3),<span style="white-space:pre"></span>'S': this.getMilliseconds()<span style="white-space:pre"></span>};    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));    for (var k in o)    if (new RegExp('(' + k + ')').test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)));    return fmt;};
2.对字符串的操作

isNaN(str);函数是判断字符能否转换为数字,若不能返回true。undefined,“”和null转换为0;

Str.indexof("str");若字符Str中存在str返回str在Str中的位置,若在第一个位置返回0.

substring(indexstart,indexend);截取字符从第indexstart位置到indexend位置的字符.如果第一个字符记为1,则截取的是不包括indexstart的字符,但是包括indexed的字符。

substr(index,length);截取从index开始长度为length的字符

3.给一组tr设定点击事件,如点击将tr的颜色设置为红色

document.getElementById('sailingItem_no').addEventListener('click',function(){var el = $(this).find('.datagrid-selected-tr td div');if(el.length < 1) return;if(sel == el[1]) return;if(sel){sel.className='';}sel = el[1];var pppp = sel.innerHTML;$("#sailing_item").val(pppp);Inin_text();sel.className='btn-red';getsailing(pppp);});
4.selected控件的使用

   1.使用Jquery得到select的value和文本值
  var txt=$(obj).find("option:selected").tex();//得到文本值
  var index=$(obj).find("option:selected").val();//得到index
  2.使用js得到select的value和文本值(沒有驗證過)
  var txt=obj.options[obj.option.selectedIndex].text;//得到文本值
  var index =obj.options[obj.options.selectedIndex].value;//得到index
在使用的時候,只需要把obj換成對應的對象,其他的不需要改變。

0 0