实用技巧:用图片代替提交和重置按钮

网络整理 - 07-27

  为了整个界面美观,我们需要对提交和重置按钮美化一番,可是无论用什么CSS样式定义按钮,内容来自站长资讯网()都很难达到满意的效果,只得用JS+图片的方式进行处理,下边我是总结出的三种方法:

  1、用图片代替提交和重置按钮,给图片加onclick事件

<script language="javascript">
function fsubmit(obj){
  obj.submit();
}
function freset(obj){
  obj.reset();
}
</script>
<form id="form1" name="form1" method="post" action="login.asp">
 姓名: <input type="text" name="textfield" />
 <img src=http://www.chinahtml.com/0708/"uploads/200707/30_145425_001.jpg" onClick="javascript:fsubmit(document.form1);">
 <img src=http://www.chinahtml.com/0708/"uploads/200707/30_145428_002.jpg" onClick="javascript:freset(document.form1);">
</form>

  2、给1的图片加上style,用于弥补方法1鼠标移到图片上无反应,未能给客户好的浏览体验

<script language="javascript">
function fsubmit(obj){
  obj.submit();
}
function freset(obj){
  obj.reset();
}
</script>
<form id="form1" name="form1" method="post" action="login.asp">
 姓名: <input type="text" name="textfield" />
 <img src=http://www.chinahtml.com/0708/"uploads/200707/30_145425_001.jpg" style="cursor:hand;" onClick="javascript:fsubmit(document.form1);">
 <img src=http://www.chinahtml.com/0708/"uploads/200707/30_145428_002.jpg" style="cursor:hand;" onClick="javascript:freset(document.form1);">
</form>

  3、用图片代替提交和重置按钮,内容来自站长资讯网()并加上链接,效果同2

<script language="javascript">
function fsubmit(obj){
  obj.submit();
}
function freset(obj){
  obj.reset();
}
</script>
<form id="form1" name="form1" method="post" action="login.asp">
 姓名: <input type="text" name="textfield" />
 <a href="javascript:fsubmit(document.form1);"><img src=http://www.chinahtml.com/0708/"uploads/200707/30_145425_001.jpg" border="0" /></a>
 <a href="javascript:freset(document.form1);"><img src=http://www.chinahtml.com/0708/"uploads/200707/30_145428_002.jpg" border="0" /></a>
</form>

  4、使用图像域。内容来自站长资讯网()由于默认图像域点击时都是提交表单,因而要进行适当的处理:

<script language="javascript">
function fsubmit(obj){
  obj.submit();
}
function freset(obj){
  obj.reset();
}
</script>
<form id="form1" name="form1" method="post" action="login.asp">
 姓名: <input type="text" name="textfield" />
 <input type="image" name="imageField" onClick="javascript:fsubmit(document.form1);return false;" src=http://www.chinahtml.com/0708/"uploads/200707/30_145425_001.jpg">
 <input type="image" name="imageField2" onClick="javascript:freset(document.form1);return false;" src=http://www.chinahtml.com/0708/"uploads/200707/30_145428_002.jpg">
</form>

要注意的是JS中表单提交和内容来自站长资讯网()重置事件后必须加(),如submit()和reset()