JavaScript常用方法汇总

发布时间:2017-01-14 00:00:00 编辑:少芬 手机版

  /* JS 替换非法字符主要用在密码验证上出现的特殊字符 */

  function URLencode(sStr) {

  return escape(sStr).replace(/\+/g, '%2B').replace(/\"/g, '%22').replace(/\'/g, '%27').replace(/\//g, '%2F');

  };

  /* 按Ctrl + Entert 直接提交表单 */

  document.body.onkeydown = function (evt) {

  evt = evt ? evt : (window.event ? window.event : null);

  if (13 == evt.keyCode && evt.ctrlKey) {

  evt.returnValue = false;

  evt.cancel = true;

  PostData();

  }

  };

  /* 获取当前时间 */

  function GetCurrentDate() {

  var d = new Date();

  var y = d.getYear()+1900;

  month = add_zero(d.getMonth() + 1),

  days = add_zero(d.getDate()),

  hours = add_zero(d.getHours());

  minutes = add_zero(d.getMinutes()),

  seconds = add_zero(d.getSeconds());

  var str = y + '-' + month + '-' + days + ' ' + hours + ':' + minutes + ':' + seconds;

  return str;

  };

  function add_zero(temp) {

  if (temp < 10) return "0" + temp;

  else return temp;

  }

  /* Js 去掉空格方法: */

  String.prototype.Trim = function()

  { return this.replace(/(^\s*)|(\s*$)/g, ""); }

  String.prototype.LTrim = function()

  {return this.replace(/(^\s*)/g, "");}

  String.prototype.RTrim = function()

  {return this.replace(/(\s*$)/g, "");}

  /* js 动态移除 head 里的 js 引用 */

  this.ClearHeadJs = function (src) {

  var js = document.getElementsByTagName('head')[0].children;

  var obj = null;

  for (var i = 0; i < js.length; i++) {

  if (js[i].tagName.toLowerCase() == "script" && js[i].attributes['src'].value.indexOf(src) > 0) {

  obj = js[i];

  }

  }

  document.getElementsByTagName('head')[0].removeChild(obj);

  };

  /* 整个UL 点击事件 加在UL里的onclick里 */

  function CreateFrom(url, params) {

  var f = document.createElement("form");

  f.setAttribute("action", url);

  for (var i = 0; i < params.length; i++) {

  var input = document.createElement("input");

  input.setAttribute("type", "hidden");

  input.setAttribute("name", params[i].paramName);

  input.setAttribute("value", params[i].paramValue);

  f.appendChild(input);

  }

  f.target = "_blank";

  document.body.appendChild(f);

  f.submit();

  };

  /* 判断浏览器使用的是哪个 JS 版本 */

  <script language="javascript">

  var jsversion = 1.0;

  </script>

  <script language="javascript1.1">

  jsversion = 1.1;

  </script>

  <script language="javascript1.2">

  jsversion = 1.2;

  </script>

  <script language="javascript1.3">

  jsversion = 1.3;

  </script>

  <script language="javascript1.4">

  jsversion = 1.4;

  </script>

  <script language="javascript1.5">

  jsversion = 1.5;

  </script>

  <script language="javascript1.6">

  jsversion = 1.6;

  </script>

  <script language="javascript1.7">

  jsversion = 1.7;

  </script>

  <script language="javascript1.8">

  jsversion = 1.8;

  </script>

  <script language="javascript1.9">

  jsversion = 1.9;

  </script>

  <script language="javascript2.0">

  jsversion = 2.0;

  </script>

  alert(jsversion);

下页更精彩:首页 上一页 1 2 3
本文已影响869
+1
0