0

我的 HTML 文档中有以下设置...

<div id="logo"><h2>my top logo</h2></div>
<div id="one"><p>this is the first section with stuff in it</p></div>
<div id="two"><h1>this is the section section with stuff in it</h1></div>
<script>//this script associates to stuff in div id=two</script>

有没有办法告诉 < div id = one >,以这种方式进入 < script > 标签下,它看起来像这样......

<div id="logo"><h2>my top logo</h2></div>
<div id="two"><h1>this is the section section with stuff in it</h1></div>
<script>//this script associates to stuff in div id=two</script>
<div id="one"><p>this is the first section with stuff in it</p></div>

我不能依赖包含所有这些的父 div 元素 id。我只需要依靠你在这里看到的。

谢谢

4

5 回答 5

2

你可以这样做,虽然我不知道这样做的目的是什么。它不应该改变页面布局的任何内容。

$("#one").insertAfter( $("#one").nextAll("script").eq(0) );

你应该修复上面的 html,你有未打开的 p 标签和未关闭的 h1 标签。

于 2012-08-21T20:32:34.717 回答
2

试试这个:

$('#two').next().after($('#one'))

小提琴

于 2012-08-21T20:33:43.267 回答
2
$('#two').next('script').after( $('#one') );
于 2012-08-21T20:33:45.967 回答
1

您可以使用以下内容来移动它们

jQuery("#one").after(jQuery("#two"));

你也可以使用

jQuery("#two").before(jQuery("#one"));

于 2012-08-21T20:33:05.053 回答
0

你可以这样做

$("#one").insertAfter($('script'));

鉴于这是您代码中唯一的脚本标记。您可以实施进一步的检查和平衡,以确保它直接位于您需要的脚本标签之后。

于 2012-08-21T20:37:35.027 回答