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()方法的區(qū)別及使用介紹
- jquery text(),val(),html()方法區(qū)別總結
- 『jQuery』.html(),.text()和.val()的概述及使用
- innerHTML與jquery里的html()區(qū)別介紹
- JQuery中html()方法使用不當帶來的陷阱
- jQuery獲取文本節(jié)點之 text()/val()/html() 方法區(qū)別
- jquery 學習之二 屬性(html()與html(val))
- jquery 的 $("#id").html() 無內容的解決方法
- jQuery html()等方法介紹
- jQuery html()方法使用不了無法顯示內容的問題
相關文章
基于jquery的動態(tài)創(chuàng)建表格的插件
工作快一年了,最近學習jquery,對jquery很感興趣。第一次寫博客,有不足之處還請各位拍磚。2011-04-04
JQuery組件基于Bootstrap的DropDownList(完整版)
這篇文章主要介紹了JQuery組件基于Bootstrap的DropDownList的完整版,在原有基礎上進行完善功能,感興趣的小伙伴們可以參考一下2016-07-07

