【发布时间】:2017-02-17 09:38:19
【问题描述】:
抱歉英语不好:P
我正在使用 ASP.NET
我正在尝试使用在 Internet 上找到的 JavaScript 为我的电话和手机文本字段设置掩码,但 JS 代码仅适用于位于我的 MasterPage 中的字段。我对 JS 没有太多经验(几乎没有),我不知道如何解决这个问题,因为我的 Logcat 没有错误。
我尝试在我的 MasterPage 的头部、正文和 ContentPlaceHolder 内部引用它。
这里是JS代码:
function mascara(o, f) {
v_obj = o
v_fun = f
setTimeout("execmascara()", 1)
}
function execmascara() {
v_obj.value = v_fun(v_obj.value)
}
function mtel(v) {
v = v.replace(/\D/g, "");
v = v.replace(/^(\d{2})(\d)/g, "($1)$2");
v = v.replace(/(\d)(\d{4})$/, "$1-$2");
return v;
}
function id(el) {
return document.getElementById(el);
}
window.onload = function () {
id('txtTelefone').onkeypress = function () { //Located at MasterPage - working
mascara(this, mtel);
}
id('txtCelular').onkeypress = function () { //Located at MasterPage - working
mascara(this, mtel);
}
id('telefoneContato').onkeypress = function () { //Located at Contact Page - not working
mascara(this, mtel);
}
id('txtCelularUser').onkeypress = function () { //Located at User Page - not working
mascara(this, mtel);
}
id('txtTelefoneUser').onkeypress = function () { //Located at User Page - not working
mascara(this, mtel);
}
}
正如我之前所说,我尝试在某些地方引用我的 JS 文件,我尝试的代码是:
<script src="<%# Page.ResolveClientUrl("../Componentes/js/mascaras.js") %>"></script>
<script src="../Componentes/js/mascaras.js"></script>
如您所见,这些字段位于不同的页面中,我也尝试将代码直接放在页面上但我没有运气。
我认为我不需要在此处发布我的整个 MasterPage,那么我将只放头部,但如果需要我将编辑帖子。
<head runat="server">
<title></title>
<link rel="shortcut icon" href="../imagens/icone.ico" type="image/x-icon" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta charset="utf-8" />
<link href="../Componentes/bootstrap/css/bootstrap.min.css" rel="stylesheet" media="screen" />
<link href="../Componentes/fontawesome/css/font-awesome.min.css" rel="stylesheet" media="screen" />
<link href="../Componentes/css/MasterPage.css" rel="stylesheet" />
<link href="../Fontes/Fontes.css" rel="stylesheet" />
<script src="../Componentes/bootstrap/js/jquery-1.12.4.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/ScrollMagic/2.0.5/ScrollMagic.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/ScrollMagic/2.0.5/plugins/debug.addIndicators.min.js"></script>
<script src="../Componentes/bootstrap/js/bootstrap.min.js"></script>
<%--Mask Scripts--%>
<script src="<%# Page.ResolveClientUrl("../Componentes/js/mascaras.js") %>"></script>
<script src="../Componentes/js/mascaras.js"></script>
<asp:ContentPlaceHolder ID="head" runat="server">
</asp:ContentPlaceHolder>
</head>
编辑 1
试过这个:
id('<%= txtTelefoneContato.ClientID %>').onkeypress = function () { //Located at Contact Page - Still not working
mascara(this, mtel);
}
id('<%= txtCelularUser.ClientID %>').onkeypress = function () { //Located at User Page - Still not working
mascara(this, mtel);
}
id('<%= txtTelefoneUser.ClientID %>').onkeypress = function () { //Located at User Page- Still not working
mascara(this, mtel);
}
对脚本的引用位于 MasterPage 头部。但是问题还是一样
EDIT2
当我检查我的页面时收到此消息:
已解决
正如 VDWWD 所解释的,我只是将 JS 代码直接放在 .aspx 页面上,并进行了以下修改:
来自:,
id('txtCelularUser').onkeypress = function () {
mascara(this, mtel);
}
到:
id('<%= txtCelularUser.ClientID %>').onkeypress = function () {
mascara(this, mtel);
}
提前谢谢你
【问题讨论】:
-
在浏览器的控制台窗口中查看联系页面。有没有错误?
-
是的,有。
mascaras.js:26 Uncaught TypeError: Cannot set property 'onkeypress' of null--- 还有这个 ---mascaras.js:20 Uncaught TypeError: Cannot set property 'onkeypress' of null -
你可以尝试使用 $(document).ready(function(){}) insted of window.load。
标签: javascript html asp.net web master-pages