jQuery html() in Firefox (uses .innerHTML) ignores DOM changes
更新时间:2010年03月05日 23:12:59 作者:
Firefox doesn't update the value attribute of a DOM object based on user input, just its valueproperty - pretty quick work around exists.
DOM:
function DisplayTextBoxValue(){
var element = document.getElementById('textbox');
// set the attribute on the DOM Element by hand - will update the innerHTML
element.setAttribute('value', element.value);
alert(document.getElementById("container").innerHTML);
return false;
}
jQuery plugin that makes .formhtml() automatically do this:
(function($) {
var oldHTML = $.fn.html;
$.fn.formhtml = function() {
if (arguments.length) return oldHTML.apply(this,arguments);
$("input,textarea,button", this).each(function() {
this.setAttribute('value',this.value);
});
$(":radio,:checkbox", this).each(function() {
// im not really even sure you need to do this for "checked"
// but what the heck, better safe than sorry
if (this.checked) this.setAttribute('checked', 'checked');
else this.removeAttribute('checked');
});
$("option", this).each(function() {
// also not sure, but, better safe...
if (this.selected) this.setAttribute('selected', 'selected');
else this.removeAttribute('selected');
});
return oldHTML.apply(this);
};
//optional to override real .html() if you want
// $.fn.html = $.fn.formhtml;
})(jQuery);
复制代码 代码如下:
function DisplayTextBoxValue(){
var element = document.getElementById('textbox');
// set the attribute on the DOM Element by hand - will update the innerHTML
element.setAttribute('value', element.value);
alert(document.getElementById("container").innerHTML);
return false;
}
jQuery plugin that makes .formhtml() automatically do this:
复制代码 代码如下:
(function($) {
var oldHTML = $.fn.html;
$.fn.formhtml = function() {
if (arguments.length) return oldHTML.apply(this,arguments);
$("input,textarea,button", this).each(function() {
this.setAttribute('value',this.value);
});
$(":radio,:checkbox", this).each(function() {
// im not really even sure you need to do this for "checked"
// but what the heck, better safe than sorry
if (this.checked) this.setAttribute('checked', 'checked');
else this.removeAttribute('checked');
});
$("option", this).each(function() {
// also not sure, but, better safe...
if (this.selected) this.setAttribute('selected', 'selected');
else this.removeAttribute('selected');
});
return oldHTML.apply(this);
};
//optional to override real .html() if you want
// $.fn.html = $.fn.formhtml;
})(jQuery);
您可能感兴趣的文章:
- jquery append()方法与html()方法的区别及使用介绍
- jquery text(),val(),html()方法区别总结
- 『jQuery』.html(),.text()和.val()的概述及使用
- innerHTML与jquery里的html()区别介绍
- JQuery中html()方法使用不当带来的陷阱
- jQuery获取文本节点之 text()/val()/html() 方法区别
- jquery 学习之二 属性(html()与html(val))
- jquery 的 $("#id").html() 无内容的解决方法
- jQuery html()等方法介绍
- jQuery html()方法使用不了无法显示内容的问题
相关文章
jquery检测input checked 控件是否被选中的方法
这篇文章主要介绍了jquery检测input checked 控件是否被选中的方法,需要的朋友可以参考下2014-03-03jQuery.validate.js表单验证插件的使用代码详解
Validate是基于jQuery的一款轻量级验证插件,内置丰富的验证规则,这篇文章主要介绍了jQuery.validate.js表单验证插件的使用代码详解,需要的朋友可以参考下2018-10-10
最新评论