jQUery Code没有'我不在IE工作
jQUery Code doesn't work in IE
这是我的jQuery代码。如果用户没有填写所有字段,它会显示一些提示消息。
$('#register').click(function(){
var sign_up_button = $(this).val();
var pass = $('#password').val();
var retype_pass = $('#retype_password').val();
if (pass != retype_pass)
{
return false;
} else if ($('#firstname').val() == '' || $('#lastname').val() == '' || $('#mobile_number').val() == '' || $('#email_address').val() == '' || $('#password').val() == '' || $('#retype_password').val() == '') {
$('#danger_container_all_fields').attr('class', 'alert alert-danger');
$('#message_fill_up_all_fields').html("Please, Fill up all fields!");
return false;
} else {
return true;
}
});
我也在网上看到了这段JS代码,用于IE中的占位符,但其中有一些冲突,抑制了我的jQuery代码的消息。
var _debug = false;
var _placeholderSupport = function() {
var t = document.createElement("input");
t.type = "text";
return (typeof t.placeholder !== "undefined");
}();
window.onload = function() {
var arrInputs = document.getElementsByTagName("input");
var arrTextareas = document.getElementsByTagName("textarea");
var combinedArray = [];
for (var i = 0; i < arrInputs.length; i++)
combinedArray.push(arrInputs[i]);
for (var i = 0; i < arrTextareas.length; i++)
combinedArray.push(arrTextareas[i]);
for (var i = 0; i < combinedArray.length; i++) {
var curInput = combinedArray[i];
if (!curInput.type || curInput.type == "" || curInput.type == "text" || curInput.type == "textarea")
HandlePlaceholder(curInput);
else if (curInput.type == "password")
ReplaceWithText(curInput);
}
if (!_placeholderSupport) {
for (var i = 0; i < document.forms.length; i++) {
var oForm = document.forms[i];
if (oForm.attachEvent) {
oForm.attachEvent("onsubmit", function() {
PlaceholderFormSubmit(oForm);
});
}
else if (oForm.addEventListener)
oForm.addEventListener("submit", function() {
PlaceholderFormSubmit(oForm);
}, false);
}
}
};
function PlaceholderFormSubmit(oForm) {
for (var i = 0; i < oForm.elements.length; i++) {
var curElement = oForm.elements[i];
HandlePlaceholderItemSubmit(curElement);
}
}
function HandlePlaceholderItemSubmit(element) {
if (element.name) {
var curPlaceholder = element.getAttribute("placeholder");
if (curPlaceholder && curPlaceholder.length > 0 && element.value === curPlaceholder) {
element.value = "";
window.setTimeout(function() {
element.value = curPlaceholder;
}, 100);
}
}
}
function ReplaceWithText(oPasswordTextbox) {
if (_placeholderSupport)
return;
var oTextbox = document.createElement("input");
oTextbox.type = "text";
oTextbox.id = oPasswordTextbox.id;
oTextbox.name = oPasswordTextbox.name;
//oTextbox.style = oPasswordTextbox.style;
oTextbox.className = oPasswordTextbox.className;
for (var i = 0; i < oPasswordTextbox.attributes.length; i++) {
var curName = oPasswordTextbox.attributes.item(i).nodeName;
var curValue = oPasswordTextbox.attributes.item(i).nodeValue;
if (curName !== "type" && curName !== "name") {
oTextbox.setAttribute(curName, curValue);
}
}
oTextbox.originalTextbox = oPasswordTextbox;
oPasswordTextbox.parentNode.replaceChild(oTextbox, oPasswordTextbox);
HandlePlaceholder(oTextbox);
if (!_placeholderSupport) {
oPasswordTextbox.onblur = function() {
if (this.dummyTextbox && this.value.length === 0) {
this.parentNode.replaceChild(this.dummyTextbox, this);
}
};
}
}
function HandlePlaceholder(oTextbox) {
if (!_placeholderSupport) {
var curPlaceholder = oTextbox.getAttribute("placeholder");
if (curPlaceholder && curPlaceholder.length > 0) {
Debug("Placeholder found for input box '" + oTextbox.name + "': " + curPlaceholder);
oTextbox.value = curPlaceholder;
oTextbox.setAttribute("old_color", oTextbox.style.color);
oTextbox.style.color = "#c0c0c0";
oTextbox.onfocus = function() {
var _this = this;
if (this.originalTextbox) {
_this = this.originalTextbox;
_this.dummyTextbox = this;
this.parentNode.replaceChild(this.originalTextbox, this);
_this.focus();
}
Debug("input box '" + _this.name + "' focus");
_this.style.color = _this.getAttribute("old_color");
if (_this.value === curPlaceholder)
_this.value = "";
};
oTextbox.onblur = function() {
var _this = this;
Debug("input box '" + _this.name + "' blur");
if (_this.value === "") {
_this.style.color = "#c0c0c0";
_this.value = curPlaceholder;
}
};
}
else {
Debug("input box '" + oTextbox.name + "' does not have placeholder attribute");
}
}
else {
Debug("browser has native support for placeholder");
}
}
function Debug(msg) {
if (typeof _debug !== "undefined" && _debug) {
var oConsole = document.getElementById("Console");
if (!oConsole) {
oConsole = document.createElement("div");
oConsole.id = "Console";
document.body.appendChild(oConsole);
}
oConsole.innerHTML += msg + "<br />";
}
}
有人能帮我找到冲突代码吗。顺便说一句,这段代码只在IE中发生冲突。在其他浏览器中,它运行得很好。
问题是这个代码:
if (oForm.attachEvent) {
oForm.attachEvent("onsubmit", function() {
PlaceholderFormSubmit(oForm);
});
}
正在替换jQuery提交处理程序。它只发生在IE中,因为attachEvent
是一个非标准的、仅IE的方法。你有几个选择来解决这个问题:
只要去掉上面的代码。那么这部分占位符代码在旧版本的IE中就不起作用了(最近的版本有
addEventListener
)。更改占位符代码中
if
和else if
块的顺序,以便首先测试addEventListener
。这样,重写只会发生在旧版本的IE中。一般来说,最好先测试新功能,只有在旧方法不存在时才使用。在占位符代码之后运行jQuery代码。这样,它将把提交处理程序添加到占位符添加的提交处理程序中。
相关文章:
- Target=_blank don'我不在mozilla和IE工作
- jQUery Code没有'我不在IE工作
- 为什么这个书签脚本是't在IE工作
- 类构造函数super()没有'我不在IE工作
- onmousemove=null不会'我不在IE工作
- execCommand('另存为',true,'data.csv');不在IE工作
- 努力让第n个有悬停的孩子在IE工作
- Javascript Groovy按钮没有'我不在IE工作
- 属性='值'jquery选择没有'我不在IE工作
- atob不在IE工作
- 将图像附加到富文本编辑器dosen'我不在IE工作
- javascript没有'I don’我一直在IE工作
- 使用插件制作移动滑动导航,但它没有'我不在IE工作
- Video.js没有'我不在FF、IE工作
- dojo on.emit”;点击“;以及“;改变“;不在IE工作
- 为什么这个工具提示没有'不在IE工作吗?错误:应为对象
- document.getElementById('a').click()没有't在IE工作
- 插入符号插件的jquery;我不在IE工作
- 聚合物:点击事件在IE工作,但不是Chrome.如何修复
- 如何使IE工作预期与正则表达式提供的函数参数在拆分函数