1.日期转换(Date)
方法一:String变为Date
var t = "2015-03-16";
var array = t.split("-");
var dt = new Date(array[0], array[1], array[2]);
// var dtt = new Date(t.replace("-g-/", ""));
方法二:String变为Date
var remindTime = "2008-04-02 10:08:44";
var str = remindTime.toString();
str = str.replace("/-/g", "/");
//// str = str.replace("T"," ");
var oDate1 = new Date(str);
方法三:String变为Date
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Date date = df.parse(StringDate);
方法四:Date变为String
Date date = new Date();
String dateStr = date.toString();
2.阻止表单提交
在JavaScript中,阻止表单默认提交行为的方法有两种,分别是:
(1) return false
<form name="loginForm" action="login.aspx" method="post">
<button type="submit" value="Submit" id="submit">Submit</button>
</form>
<script>
var submitBtn = document.getElementById("submit");
submitBtn.onclick = function (event) {
alert("preventDefault!");
return false;
};
</script>
(2) 使用preventDefault()
在标准浏览器中,阻止浏览器默认行为使用event.preventDefault(),而在IE6~8中,使用returnValue属性来实现。
示例代码
<form name="loginForm" action="login.aspx" method="post">
<button type="submit" value="Submit" id="submit">Submit</button>
</form>
<script>
var submitBtn = document.getElementById("submit");
submitBtn.onclick = function (event) {
alert("preventDefault!");
var event = event || window.event;
event.preventDefault(); // 兼容标准浏览器
window.event.returnValue = false; // 兼容IE6~8
};