[英]JQuery append() doesn't append
I've got html structure like this: 我有这样的html结构:
<div id="things">
<div class="whatever">
<div class="frame">content</div>
<div class="frame">content</div>
<div class="frame">content</div>
</div>
</div>
And I got JS with JQuery script that works on click on button on that page: 我使用JQuery脚本获得了JS,该脚本可以在该页面上点击按钮:
function intsaver(){
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("POST", "intsaver.php", true);
var tosf = $('<div></div>');
$("#things").find(".frame").each(function(){
tosf.append($(this).innerHTML);
alert(''+tosf);
});
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("data=" + tosf.innerHTML);
}
I can see this
contents in debugger, they are exactly what I'm looking for. 我可以在调试器中看到
this
内容,它们正是我正在寻找的。 But tosf
remains undefined
and on server side I recieve undefined
. 但是
tosf
仍未undefined
,在服务器端我收到undefined
。
I've tried some ways, for examle, I appended this
itself. 我已经尝试了一些方法,例如,我自己附加了
this
。 As result, div's disappeared from page, but tosf
remained undefined
. 结果,div从页面上消失了,但是
tosf
仍未undefined
。
I believe I've made some obvious mistake. 我相信我犯了一些明显的错误。
Change 更改
tosf.append($(this).innerHTML);
To 至
tosf.append($(this).html());//$(this) a jQuery object
Or 要么
tosf.append(this.innerHTML);//this a dom object
$(this)
is a jQuery object
not a dom object
which doesn't have property innerHTML
.Use .html()
instead. $(this)
是一个jQuery object
而不是dom object
,它没有属性innerHTML
。使用.html()
代替。
Try 尝试
function intsaver() {
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("POST", "intsaver.php", true);
var tosf = $('<div></div>');
$("#things").find(".frame").each(function () {
tosf.append(this.innerHTML); //innerHTML is a property dom element
alert('' + tosf);
});
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send("data=" + tosf.html()); //tosf is a jQuery object os call .html()
}
$(this).innerHTML
should be $(this).html();
$(this).innerHTML
应为$(this).html();
and tosf.innerHTML
should be tosf.html()
和
tosf.innerHTML
应该是tosf.html()
Reason is tosf
and $(this)
are jQuery style and innerHTML
is pure javascript . 原因是
tosf
和$(this)
是jQuery样式, innerHTML
是纯javascript 。
A bit more explanation 'why .innerHTML
' did not work. 更多解释'为什么
.innerHTML
'不起作用。 As it is stated in the answers, $(this).innerHTML
would not work as jQuery does not have that property. 正如答案中所述,
$(this).innerHTML
不起作用,因为jQuery没有该属性。 But if you tweak your code you can benefit from .innerHTML
as well. 但是如果你调整你的代码,你也可以从
.innerHTML
受益。 If you wrap an element with $(element)
it creates jQuery specific object and you will need to get the element from it if you want to use .innerHTML
like: $(this)[0].innerHTML
. 如果用
$(element)
包装元素,它会创建特定于jQuery的对象,如果你想使用.innerHTML
,你需要从中获取元素: $(this)[0].innerHTML
。 As the this
is only element in that jQuery array, this.innerHTML
will be sufficient, without making $(this)
. 因为
this
只是jQuery数组中的元素, this.innerHTML
就足够了,而不需要生成$(this)
。 so your code can be: 所以你的代码可以是:
function intsaver(){
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("POST", "intsaver.php", true);
var tosf = $('<div></div>');
$("#things").find(".frame").each(function(){
tosf.append(this.innerHTML);
alert(''+tosf);
});
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("data=" + tosf[0].innerHTML);}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.