复制代码 代码如下:
<form action="https://www.jb51.net">
<input type="text" />
<input type="submit" />
</form>
<script>
var code = function () {
return false;
};
var element = window.document.getElementById("form");
if (element.addEventListener) {
element.addEventListener("submit", code, false);
}
</script>
修改后的Code:
复制代码 代码如下:
<form action="https://www.jb51.net">
<input type="text" />
<input type="submit" value="Submit" />
</form>
<script>
var code = function (eventObject) {
if (eventObject.preventDefault) {
eventObject.preventDefault();
} else if (window.event) /* for ie */ {
window.event.returnValue = false;
}
return true;
};
window.onload = function(){
var element = window.document.getElementById("form");
if (element.addEventListener) {
element.addEventListener("submit", code, false);
} else if (element.attachEvent) {
element.attachEvent("onsubmit", code);
}
}
</script>
备注:
1:preventDefault函数的使用请参考 https://developer.mozilla.org/en/DOM/event.preventDefault
2:DOM Events :
您可能感兴趣的文章: