2

我在外部文件 (test.html) 中有一个内容 (#content) 以加载到 index.html 的另一个 DIV (#result) 中:

测试.html:

<div id="content">This text should not be loaded with its DIV. <strong> May contain other tags</strong></div>

jQuery:

$('#result').load('test.html #content');

index.html 结果,出乎意料:

<div id="result"><div id="content">This text should not be loaded with its DIV. <strong> May contain other tags</strong></div></div>

index.html 结果,预期:

<div id="result">This text should not be loaded with its DIV. <strong> May contain other tags</strong></div>

你如何只加载#content 的实际内容/ HTML,它也可能包含其他标签,但只加载没有它的包装器 DIV (#content)?原因是为了简单地避免不必要的 div,它们也可能与其他样式的 DIV 错误地发生冲突。

非常感谢任何提示。谢谢

4

3 回答 3

1

如果您能够将 .load() 的结果转换为 var。你可以这样做。

var result = ... load line ...
var outResult = $(result);
// now strip the div out of outResult using jquery......
outResult = StripContentDiv(outResult);
$('#result').html(outResult);

对不起,我不能更具体,因为我不是 jquery 专家。

于 2012-04-20T01:38:53.587 回答
1

这是一个想法。如果您使用临时标签作为工作区怎么办?

这个 jQuery 的 javascript...

$('#workarea').load('test.html #content');

...可能会产生这个结果。请注意,工作区是隐藏的:

<div id="result"></div>
<div id="workarea" style="display: none"><div id="content>This text should not be loaded with its DIV</div></div>

然后,您可以将其移至结果...

$('#result').html($('#workarea').html());

它应该产生这个结果。

<div id="result">This text should not be loaded with its DIV</div>
<div id="workarea" style="display: none"><div id="content></div></div>

编辑:

这是一个完整的脚本:

$('#workarea').load('test.html #content');
$('#result').html($('#workarea').html());

您将使用这两行而不是您的一行。如果我理解正确,它应该会在上面的查询中产生预期的结果。

于 2012-04-20T01:42:03.453 回答
1

是你看的吗

$(document).ready(function(){
  $('#result').load('test.html #content', function () {
    $('#content', this).text;
     });
   }); 

:P xD 由 JNE

于 2012-04-20T02:00:35.867 回答