如何在特定元素上使用.load时仅检索元素的内容?
问题描述:
例如,当我做这样的事情:如何在特定元素上使用.load时仅检索元素的内容?
$('#foo').load(window.location + ' #foo');
人们期望代码文提及与foo
的ID重新加载div的内容。但不是。
但它实际上给了我,如果foo
内容是:
<div id="foo">
Foofoo
</div>
它会给我:
<div id="foo">
<div id="foo"><!-- Retrieved from the other page, don't want this -->
Foofoo
</div><!-- Don't want this either -->
</div>
但我只想在div foo
的内容。我不想添加任何子元素,所以我只是想知道是否可以将配置更改为仅加载内容,或者如果可能的话以其他方式。
答
要使用什么是.html()
。
$('#foo').load($('#foo').html());
答
然后尝试这样的:
my_text = $('#foo').text();
$('#foo').load(window.location + my_text);
+0
这是工作.. ?? – Gautam3164
与downvote什么?我没有解释清楚吗? – think123