在本文中,您将会了解到关于jQueryhtml的新资讯,同时我们还将为您解释剥离脚本标签的相关在本文中,我们将带你探索jQueryhtml的奥秘,分析剥离脚本标签的特点,并给出一些关于Firefox中
在本文中,您将会了解到关于jQuery html的新资讯,同时我们还将为您解释剥离脚本标签的相关在本文中,我们将带你探索jQuery html的奥秘,分析剥离脚本标签的特点,并给出一些关于Firefox中的jQuery html()(使用.innerHTML)忽略DOM更改、html中的input标签的checked属性jquery判断代码_jquery、innerHTML与jquery里的html()区别介绍_jquery、JavaScript / jQuery:如何获取HTML并显示HTML,包括标签的实用技巧。
本文目录一览:- jQuery html()剥离脚本标签
- Firefox中的jQuery html()(使用.innerHTML)忽略DOM更改
- html中的input标签的checked属性jquery判断代码_jquery
- innerHTML与jquery里的html()区别介绍_jquery
- JavaScript / jQuery:如何获取HTML并显示HTML,包括标签
jQuery html()剥离脚本标签
我需要用ajax调用产生的html替换页面中div的内容。问题是html中包含一些必要的脚本,并且jquery
html()函数似乎将它们剥离了,我需要过滤响应并仅获取特定的div。
我正在考虑一种解决方法,该方法是从ajax响应中提取所有脚本标签,然后将其附加到DOM中,但是这样做很麻烦。
这是我的代码;
$('a.link-vote').live('click',function(){
var idfeedback = $(this).attr('id').split('-')[1];
var href = $(this).attr('href');
$('.feedback-' + idfeedback + '-loader').show();
$.ajax({
type: "POST",url: href,success: function(response){
var x = $(response).find('#feedback-'+ idfeedback).html();
$('.feedback-' + idfeedback + '-loader').hide();
$('#feedback-'+ idfeedback).html(x);
}
});
return false;
});
但这是任何结论。我尝试了那里提出的解决方案,但没有一个起作用。
编辑:我似乎找到了一个基于该旧主题的解决方法,但它并不漂亮。
var dom = $(response);
// var x = $(response).find('#feedback-'+ idfeedback).html();
$('.feedback-' + idfeedback + '-loader').hide();
//$('#feedback-'+ idfeedback).html(x);
$('#feedback-'+ idfeedback).html(dom.find('#feedback-'+ idfeedback).html());
dom.filter('script').each(function(){
var obj = $(this);
$('#feedback-'+ idfeedback + ' .feedback-comments').append(obj);
});
必须有一个简单的方法。
Firefox中的jQuery html()(使用.innerHTML)忽略DOM更改
我真的很惊讶,我之前从未遇到过这个问题,但是似乎在元素上调用jQueries
.html()函数会忽略DOM中的更改,即它会返回原始源中的HTML。IE不会这样做。jQueries .html()仅在内部使用innerHTML属性。
这是要发生吗?我使用的是Firefox 3.5.2。我在下面有一个示例,无论您将文本框的值更改为什么,“
container”元素的innerHTML都只会返回HTML标记中定义的值。该示例并没有使用jQuery只是为了使其更简单(使用jQuery的结果是相同的)。
有谁能解决我可以在哪个位置获取容器html的当前状态,即包括对DOM的任何脚本化更改?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><html xmlns="http://www.w3.org/1999/xhtml" > <head> <script type="text/javascript"> <!-- function BodyLoad(){ document.getElementById("textbox").value = "initial UPDATE"; DisplayTextBoxValue(); } function DisplayTextBoxValue(){ alert(document.getElementById("container").innerHTML); return false; } //--> </script> </head> <body onload="BodyLoad();"> <div id="container"> <input type="text" id="textbox" value="initial" /> </div> <input type="button" id="button" value="Test me" onclick="return DisplayTextBoxValue();" /> </body></html>
答案1
小编典典Firefox不会根据用户输入来更新DOM对象的value
属性 ,而只是基于其value
属性 -存在非常快速的解决方法。
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;}
.formhtml()
自动执行此操作的jQuery插件:
(function($) { var oldHTML = $.fn.html; $.fn.formhtml = function() { if (arguments.length) return oldHTML.apply(this,arguments); $("input,button", this).each(function() { this.setAttribute(''value'',this.value); }); $("textarea", this).each(function() { // updated - thanks Raja & Dr. Fred! $(this).text(this.value); }); $("input:radio,input: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);
html中的input标签的checked属性jquery判断代码_jquery
虽然会有这样的错误,但是如果再仔细测试一下,会发现input标type属性设为button时,还是具有checked属性的,而且默认为false 代码如下所示:
innerHTML与jquery里的html()区别介绍_jquery
看个示例:
var tbody=document.createElement(''tbody'');
tbody.innerHTML=''IE下tbody的innerHTML是只读的''; //在IE下报错,目标对象错误