2

外部ファイル (test.html) にコンテンツ (#content) があり、index.html の別の DIV (#result) にロードします。

test.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 と競合する可能性がある不要な 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>

編集:

完全なスクリプトをすべて 1 か所にまとめたものを次に示します。

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

1 行の代わりに、これらの 2 行を使用します。私の理解が正しければ、上記のクエリで期待される結果が得られるはずです。

于 2012-04-20T01:42:03.453 に答える