js去掉前后空格
$.trim(" a
")
onclick事件:
<button type="button"
onclick="alert(‘Welcome!‘)">点击这里</button>
document.write("<h1>This is a heading</h1>");
document.getElementById("demo").innerHTML="My First JavaScript";
获取
getElementById("demo")
getElementTagName();
getElementByClass("demo")
定义方法
<script>
function myFunction()
{
document.getElementById("demo").innerHTML="My First
JavaScript Function";
}
</script>
时间和日期
d=new Date().getDay()
d.getTime() => 毫秒
d.setFullYear(1992,10,3) => 设置时间
d.getDay() => 获取星期
var today=new Date()
var h=today.getHours()
var m=today.getMinutes()
var s=today.getSeconds()
for循环
for (var i=0;i<cars.length;i++)
{
document.write(cars[i] + "<br>");
}
var person={fname:"John",lname:"Doe",age:25};
for (x in person)
{
txt=txt + person[x];
}
for (x in person)
{
txt=txt + x;
}
字符串匹配和替换
apos = value.indexOf("@")
dotpos = value.lastIndexOf(".")
document.write(str.indexOf("Hello") + "<br />")
0/-1
document.write(str.match("world") + "<br />") word/null
str.replace(/Microsoft/,"W3School") Visit W3School!
设置焦点
email.focus()
创建dome
var para=document.createElement("p");
var node=document.createTextNode("这是新段落。");
para.className = "clear";
para.setAttribute(‘id‘, ‘graph‘)
para.appendChild(node);
var element=document.getElementById("div1");
element.appendChild(para);
设置dome属性
document.getElementById(id).attribute=new value
document.getElementById("image").src="landscape.jpg";
document.getElementById("p2").style.color="blue";
获取dome属性
getAttribute
oInput.getAttribute(‘value‘)
检测浏览器是否启用了cookie
navigator.cookieEnabled==true
input值改变
onchange()
转为大写
toUpperCase()
延时执行
t = setTimeout(‘startTime()‘,500)
数组合并
arr.concat(arr2)
arr.join(".")
四舍五入
Math.round(0.60)
取整
Math.floor(2.6)
随即数
Math.random()
字符串
str = ‘abcdesff‘
str = substring(2,4) => cde
shift按下,禁止a标签
function cancelLink() {
if (window.event.srcElement.tagName == "A" &&
window.event.shiftKey)
window.event.returnValue = false;
}
js 重新加载页面
<script>location.reload();</script>
js获取随机时间戳 (new Date()).getTime()
bind()
一次绑定多个事件
$(‘#foo‘).bind({
click: function() {
// do something on click
},
mouseenter: function() {
// do something on mouseenter
}
});
on()动态加载dome结果执行js事件,ajax中get请求
$(‘.mba_business‘).on(‘click‘,
‘.next_following_companies‘, function() {
var $this = $(this);
var page = $this.attr(‘p‘);
if (page ){
$.get("ajax/following/companies/feed", {current_user_id : <%= current_user_id
%>, page : page, t:(new Date()).getTime() }, function (data) {
if (data.success)
$this.parent().empty().append(data.html);
}, ‘json‘);
}
});
js笔记,古老的榕树,5-wow.com