js常用代码
=== js任意范围随机数发生器 ===
Math.random()*(n-m)+m //产生0-1 之间随机数,并通过系数变换到m-n 之间
javascript:history.go(-1) //
$(obj).parent().parent().attr('class','color_a');//jquery获取上级元素
//js中的ajax同步或异步
synchronization n.同一时刻;同步;使时间互相一致;同时性
asynchronous adj.异步的
“async:false,”中的async是"异步的"的简写,js的ajax中这个值默认是true
#js正则验证
if(!/^[A-Za-z0-9]+$/.test(name))
{
alert("名称只能为字母或数字");
return false;
}
var regMYSQLERROR = new RegExp("MySQL connect ERROR","ig");
if (data.match(regMYSQLERROR)) {
alert("数据库连接错误!");
}
#jquery中通过的name选择input元素
$("input[name='specialDate']");
#代码摘录
var mouseover_tid = [];
var mouseout_tid = [];
$(document).ready(function(){
$('.main_top_x > ul > li').each(function(index){
$(this).hover(
function(){
var _self = this;
clearTimeout(mouseout_tid[index]);
mouseover_tid[index] = setTimeout(function() {
$(_self).find('a:eq(0)').css("background","url(images/nav_bg.jpg) no-repeat center top");
$(_self).find('a:eq(0)').css("color","#000000");
$(_self).find('ul:eq(0)').slideDown(300);
}, 10);
},
function(){
var _self = this;
clearTimeout(mouseover_tid[index]);
mouseout_tid[index] = setTimeout(function() {
$(_self).find('a:eq(0)').css("background","url(images/nav_bg1.jpg) no-repeat center top");
$(_self).find('a:eq(0)').css("color","#ffffff");
$(_self).find('ul:eq(0)').slideUp(250);
}, 10);
}
);
});
$(".main_top_x ul li ul li a").each(function(){
$(this).hover(
function(){
$(this).css("background","url(images/nav_bg2.jpg) repeat-x center top;");
},
function(){
$(this).css("background","none");
}
);
});
});
#js弹窗组件
http://layer.layui.com
#Jquery中ajax使用实例
$.ajax({
url:"/user/login",
type:'post',
async: false,
data:{'username':'zhangsan'},
success:function(data){
/*if(data.code == '1'){
//code here
}else{
alert(data.error_msg);
}*/
}
});
#js中"!function(e){}()"这种结构是表示自动执匿名函数,最后一个括号中的内容作为匿名函数的参数 [1]
#JS事件触发
export default function triggerEvent(el, type) {
if ('createEvent' in document) {
// modern browsers, IE9+
const e = document.createEvent('HTMLEvents');
e.initEvent(type, false, true);
el.dispatchEvent(e);
}
}
#Q: 某些情况下console.log()不能打印array或者object怎么办? A: 转换成json字符串再打印。
function log(msg, level) {
level = level || "log";
if (typeof msg === "object") {
msg = JSON.stringify(msg, null, " ");
}
console.log(msg);
if (level === "status" || level === "error" || 1) {
var msgDiv = document.createElement("div");
msgDiv.textContent = msg;
if (level === "error") {
msgDiv.style.color = "red";
}
msgDiv.style.padding = "5px 0";
msgDiv.style.borderBottom = "rgb(192,192,192) solid 1px";
document.getElementById("output").appendChild(msgDiv);
}
}
#ES6中可以直接引入css?
//import './index.css';
#js取url中文件名或扩展名
function getUrlExt(url){
return url.substring(url.lastIndexOf(".")+1, url.length);
}
function getUrlFile(url){
return url.substring(url.lastIndexOf("/")+1, url.length);
}
#取url中的查询参数
function getUrlParams(url) {
var params = {};
url.replace(/[?&]+([^=&]+)=([^&]*)/gi, function (str, key, value) {
params[key] = value;
});
return params;
}
#JSON.stringify的第三个参数可以格式化输出字符串 [1]
var str = JSON.stringify({'aaaa':'bbbbbb','c':'dddddddddd'}, null, " ");
console.log(str);
输出字符串:
'{
"aaaa": "bbbbbb",
"c": "dddddddddd"
}'
#js中定时器常用模版
#同一时刻可能有多个请求
var timer = setInterval(function(){
console.log('a');
}, 3 * 1000);
clearInterval(timer);
#同一时刻只有一个请求
function start_all_refresh()
{
console.log('a');
setTimeout(all_refresh, 3 * 1000);
}
function all_refresh()
{
start_all_refresh();
}
#动态加载js文件 [1]
var script=document.createElement("script");
script.type="text/javascript";
script.src="/js/lib/jquery.min.js";
document.getElementsByTagName('head')[0].appendChild(script);
script.onload=function(){
console.log('ok');
}
posted on 2013-06-14 23:04 dream_bccb 阅读(272) 评论(0) 收藏 举报
浙公网安备 33010602011771号