下面input格式化输出的JS语句调用有错吗,请高人示正,谢谢!
function ()
{
function format_card(s)
{
if (s.length == 0) {
return;
}
s = s.replace(/[\s\xa0\u3000]/g, "");
s = s.split("");
var result = [];
for (var i = 0, el; el = s[i]; i++) {
if (i > 0 && i % 4 == 0) {
result[result.length] = " ";
}
result[result.length] = el;
}
return result.join("");
}
function kd()
{
var self = this;
setTimeout(function ()
{
var ps = self.previousSibling;
var v = $.trim(self.value);
if (v == "") {
ps.style.display = "none";
return;
}
ps.style.display = "";
ps.innerHTML = format_card(v);
}, 0);
}
function bl()
{
this.previousSibling.style.display = "none";
}
function fc()
{
var ps = this.previousSibling;
var inputPos = $(this).position();
if (!ps || ps.className != "card_number_tip_div")
{
ps = $("<div class='card_number_tip_div' style='position:absolute;left:" + inputPos.left + "px;top:" + (inputPos.top - 25) + "px;background: none repeat scroll 0 0 #FFFFFF;border: 1px solid #C5C5C5;" + "width:auto !important;width:" + (this.offsetWidth - 12) + "px;" + "min-width:" + (this.offsetWidth - 12) + "px;font:600 14px Arial;padding:5px 5px 2px;white-space:nowrap;display:none;'></div>")[0];
this.parentNode.insertBefore(ps, this);
}
var v = $.trim(this.value);
if (v != "") {
ps.innerHTML = format_card(v);
ps.style.display = "";
}
}
$(".card_number_spaces").keydown(kd).focus(fc).each(function ()
{
this.onblur = bl;
}).attr("autocomplete", "off");
}
function ()
{
function format_card(s)
{
if (s.length == 0) {
return;
}
s = s.replace(/[\s\xa0\u3000]/g, "");
s = s.split("");
var result = [];
for (var i = 0, el; el = s[i]; i++) {
if (i > 0 && i % 4 == 0) {
result[result.length] = " ";
}
result[result.length] = el;
}
return result.join("");
}
function kd()
{
var self = this;
setTimeout(function ()
{
var ps = self.previousSibling;
var v = $.trim(self.value);
if (v == "") {
ps.style.display = "none";
return;
}
ps.style.display = "";
ps.innerHTML = format_card(v);
}, 0);
}
function bl()
{
this.previousSibling.style.display = "none";
}
function fc()
{
var ps = this.previousSibling;
var inputPos = $(this).position();
if (!ps || ps.className != "card_number_tip_div")
{
ps = $("<div class='card_number_tip_div' style='position:absolute;left:" + inputPos.left + "px;top:" + (inputPos.top - 25) + "px;background: none repeat scroll 0 0 #FFFFFF;border: 1px solid #C5C5C5;" + "width:auto !important;width:" + (this.offsetWidth - 12) + "px;" + "min-width:" + (this.offsetWidth - 12) + "px;font:600 14px Arial;padding:5px 5px 2px;white-space:nowrap;display:none;'></div>")[0];
this.parentNode.insertBefore(ps, this);
}
var v = $.trim(this.value);
if (v != "") {
ps.innerHTML = format_card(v);
ps.style.display = "";
}
}
$(".card_number_spaces").keydown(kd).focus(fc).each(function ()
{
this.onblur = bl;
}).attr("autocomplete", "off");
}
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<link rel="shortcut icon" href="/img/favicon.ico" type="image/x-icon" />
<link rel="icon" href="/img/favicon.ico" type="image/x-icon" />
<title>中国银联在线支付认证支付</title><link rel="stylesheet" type="text/css" href="./Opt_static_-1557991915_1.1.3beta1.css" />
<script type="text/javascript" src="./Opt_static_161764658_1.1.3beta1.js"></script>
</head>
<body>
<table class="input-table" width="100%" style="table-layout: fixed">
<tr>
<th valign="middle">银行卡号:</th>
<td id="cardNumberTd"><input id="cardNumber" maxlength="19" name="cardNumber" class="long card_number_spaces" type="text" /> </td>
</tr>
<tr>
<th valign="middle">银行卡号:</th>
<td id="cardNumberTd"><input id="cardNumber" maxlength="19" name="cardNumber" class="long card_number_spaces" type="text" /> </td>
</tr>
</table>
</body>
</html>
.input-table td{padding:6px 0}
.input-table td input.long{width:166px}
.input-table .refreshPage{color:#f63}
.input-table .refreshPageBtn{color:#f63;font-weight:bold;text-decoration:underline}
.tab_debit td input.long{width:166px}
{
this.onblur = bl;
})
干吗还要each一下 直接.blur有的啊.
另外还要考虑鼠标复制粘贴的问题不光是key事件
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<link rel="shortcut icon" href="/img/favicon.ico" type="image/x-icon" />
<link rel="icon" href="/img/favicon.ico" type="image/x-icon" />
<title>中国银联在线支付认证支付</title>
<style type="text/css">
.input-table th { text-align: right; padding: 8px 0; font-size: 12px; font-weight: normal; }
.input-table td { padding: 6px 0; }
.input-table td input.long { width: 166px; }
.input-table .refreshPage { color: #f63; }
.input-table .refreshPageBtn { color: #f63; font-weight: bold; text-decoration: underline; }
.tab_debit td input.long { width: 166px; }
</style> <script type="text/javascript" src="../javascript/jquery/jquery-1.7.min.js"></script> <script type="text/javascript">
$(function ()
{
function format_card(s)
{
if (s.length == 0) {
return;
}
s = s.replace(/[\s\xa0\u3000]/g, "");
s = s.split("");
var result = [];
for (var i = 0, el; el = s[i]; i++) {
if (i > 0 && i % 4 == 0) {
result[result.length] = " ";
}
result[result.length] = el;
}
return result.join("");
}
function kd()
{
var self = this;
setTimeout(function ()
{
var ps = self.previousSibling;
var v = $.trim(self.value);
if (v == "") {
ps.style.display = "none";
return;
}
ps.style.display = "";
ps.innerHTML = format_card(v);
}, 0);
}
function bl()
{
this.previousSibling.style.display = "none";
}
function fc()
{
var ps = this.previousSibling;
var inputPos = $(this).position();
if (!ps || ps.className != "card_number_tip_div")
{
ps = $("<div class='card_number_tip_div' style='position:absolute;left:" + inputPos.left + "px;top:" + (inputPos.top - 25) + "px;background: none repeat scroll 0 0 #FFFFFF;border: 1px solid #C5C5C5;" + "width:auto !important;width:" + (this.offsetWidth - 12) + "px;" + "min-width:" + (this.offsetWidth - 12) + "px;font:600 14px Arial;padding:5px 5px 2px;white-space:nowrap;display:none;'></div>")[0];
this.parentNode.insertBefore(ps, this);
}
var v = $.trim(this.value);
if (v != "") {
ps.innerHTML = format_card(v);
ps.style.display = "";
}
}
$(".card_number_spaces").keydown(kd).focus(fc).blur(bl).attr("autocomplete", "off");
});
</script></head>
<body>
<table class="input-table" width="100%" style="table-layout: fixed">
<tr>
<th valign="middle">
银行卡号:
</th>
<td id="cardNumberTd">
<input id="cardNumber" maxlength="19" name="cardNumber" class="long card_number_spaces"
type="text" />
</td>
</tr>
<tr>
<th valign="middle">
银行卡号:
</th>
<td id="cardNumberTd">
<input id="cardNumber" maxlength="19" name="cardNumber" class="long card_number_spaces"
type="text" />
</td>
</tr>
</table>
</body>
</html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<link rel="shortcut icon" href="/img/favicon.ico" type="image/x-icon" />
<link rel="icon" href="/img/favicon.ico" type="image/x-icon" />
<title>中国银联在线支付认证支付</title>
<style type="text/css">
.input-table th { text-align: right; padding: 8px 0; font-size: 12px; font-weight: normal; }
.input-table td { padding: 6px 0; }
.input-table td input.long { width: 166px; }
.input-table .refreshPage { color: #f63; }
.input-table .refreshPageBtn { color: #f63; font-weight: bold; text-decoration: underline; }
.tab_debit td input.long { width: 166px; }
</style> <script type="text/javascript" src="../javascript/jquery/jquery-1.7.min.js"></script> <script type="text/javascript">
$(function ()
{
function format_card(s)
{
if (s.length == 0) {
return;
}
s = s.replace(/[\s\xa0\u3000]/g, "");
s = s.split("");
var result = [];
for (var i = 0, el; el = s[i]; i++) {
if (i > 0 && i % 4 == 0) {
result[result.length] = " ";
}
result[result.length] = el;
}
return result.join("");
}
function kd()
{
var self = this;
setTimeout(function ()
{
var ps = self.previousSibling;
var v = $.trim(self.value);
if (v == "") {
ps.style.display = "none";
return;
}
ps.style.display = "";
ps.innerHTML = format_card(v);
}, 0);
}
function bl()
{
this.previousSibling.style.display = "none";
}
function fc()
{
var ps = this.previousSibling;
var inputPos = $(this).position();
if (!ps || ps.className != "card_number_tip_div")
{
ps = $("<div class='card_number_tip_div' style='position:absolute;left:" + inputPos.left + "px;top:" + (inputPos.top - 25) + "px;background: none repeat scroll 0 0 #FFFFFF;border: 1px solid #C5C5C5;" + "width:auto !important;width:" + (this.offsetWidth - 12) + "px;" + "min-width:" + (this.offsetWidth - 12) + "px;font:600 14px Arial;padding:5px 5px 2px;white-space:nowrap;display:none;'></div>")[0];
this.parentNode.insertBefore(ps, this);
}
var v = $.trim(this.value);
if (v != "") {
ps.innerHTML = format_card(v);
ps.style.display = "";
}
}
$(".card_number_spaces").keydown(kd).focus(fc).blur(bl).attr("autocomplete", "off");
});
</script></head>
<body>
<table class="input-table" width="100%" style="table-layout: fixed">
<tr>
<th valign="middle">
银行卡号:
</th>
<td id="cardNumberTd">
<input id="cardNumber" maxlength="19" name="cardNumber" class="long card_number_spaces"
type="text" />
</td>
</tr>
<tr>
<th valign="middle">
银行卡号:
</th>
<td id="cardNumberTd">
<input id="cardNumber" maxlength="19" name="cardNumber" class="long card_number_spaces"
type="text" />
</td>
</tr>
</table>
</body>
</html>没有jquery引用 并且少了$()