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

2016-02-19 13:33 1 1 收藏

有了下面这个jQuery html() in Firefox (uses .innerHTML) ignores DOM chang教程,不懂jQuery html() in Firefox (uses .innerHTML) ignores DOM chang的也能装懂了,赶紧get起来装逼一下吧!

【 tulaoshi.com - Web开发 】

DOM:

代码如下:

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/webkaifa/)(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/webkaifa/)
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:

代码如下:

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/webkaifa/)(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/webkaifa/)
(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);

来源:https://www.tulaoshi.com/n/20160219/1604615.html

延伸阅读
标签: Web开发
我们已经学习过如何来选取DOM元素,怎么创建数组,怎么创建函数,怎么把事件添加到元素,今天我们来深入地学习一下如果操纵HTML元素。通过MooTools 1.2,你可以添加新元素到一个HTML页面中,也可以删除元素,以及改变任何样式或者元素参数,这些都非常容易。 基本方法 .get(); 这个工具可以让你获取元素的属性(property)。元素的属性是...
标签: Web开发
对于获取某一个元素的值,如input框,我们往往是用$('#test').val(); $('#test')这个我就不多说了,反正就是获取ID为test的这个元素。 $('#test').val(),也就是获取它的值,一般来说,凡是能够用在FORM里的元素,都可以用.val()来进行值的获取,如input,textarea,select等,都可以用.val()来获取它们的当前值 而.val('aa');则是设置该元...
标签: Web开发
12 设置和获取HTML、文本和值 $("selector").html() 获取html代码 $("selector").html(html) 设置html代码(替换标签中内容),html()方法不可用于XML文档 $("selector").text() 获取纯文本内容 $("selector").text(text) 设置文本内容(替换标签中内容),text()方法可以用于XML文档 $("selector").val() 获取元素的值 $("selector...
标签: Web开发
IE与火狐下可以跑. //回车事件处理 $(document).keydown(function(event){ if(event.keyCode==13){ alert($('#idone').val()); //回车键盘ISBN获取 if($('#idone').val()=='isbn'){ //isbncheck(); $('#ztm').focus(); }else if($('#idone').val()=='ztm'){ $('#bltm').focus(); } if($('#idone').val()=='bltm'){ $('#ftm').fo...
标签: Web开发
那么,我需要一个方法转义。网上搜了一下,大部分是自己编写一个正则替换。不过我的习惯是尊重既有的资源。有人提出可以用dom的功能。先作为innerTEXT传给一个dom对象,再取innerHTML属性,就可以取到转义后的文本了。 方法不错,不过写法上,有没有取巧的办法呢? JQuery社区有人给出了办法:假设有文本 context,可以对一个jQuery对象 $(x...

经验教程

424

收藏

99
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部