教程集 www.jiaochengji.com
教程集 >  jQuery  >  jquery 教程  >  正文 jQuery html() in Firefox (uses .innerHTML) ignores DOM changes

jQuery html() in Firefox (uses .innerHTML) ignores DOM changes

发布时间:2013-08-26   编辑:jiaochengji.com
Firefox doesn't update the value attribute of a DOM object based on user input, just its valueproperty - pretty quick work around exists.
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);

您可能感兴趣的文章:
jQuery html() in Firefox (uses .innerHTML) ignores DOM changes
jDrop Image Combo
解密jQuery内核 DOM操作方法(二)html,text,val
jQuery对象与DOM对象之间的转换方法
jQuery 开发者应该注意的9个错误
innerHTML与jquery里的html()区别介绍
jquery Slideshow
Tree
JQUERY 对象与DOM对象之两者相互间的转换
jQuery相册展示插件 EOGallery

[关闭]
~ ~