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檢測input checked 控件是否被選中的方法
這篇文章主要介紹了jquery檢測input checked 控件是否被選中的方法,需要的朋友可以參考下2014-03-03用戶管理的設計_jquery的ajax實現(xiàn)二級聯(lián)動效果
下面小編就為大家?guī)硪黄脩艄芾淼脑O計_jquery的ajax實現(xiàn)二級聯(lián)動效果。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-07-07jQuery.validate.js表單驗證插件的使用代碼詳解
Validate是基于jQuery的一款輕量級驗證插件,內置豐富的驗證規(guī)則,這篇文章主要介紹了jQuery.validate.js表單驗證插件的使用代碼詳解,需要的朋友可以參考下2018-10-10jQuery序列化form表單數據為JSON對象的實現(xiàn)方法
這篇文章主要介紹了jQuery序列化form表單數據為JSON對象的實現(xiàn)方法,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2018-09-09