javascript - How to Move Element With Script Element Without Re-Executing Script with jQuery? - Stack Overflow

I have this code snippet:<div id="div1"><div><div id="div2"><

I have this code snippet:

  <div id="div1">
  </div>

  <div id="div2">
    <h3>This is the content</h3>
    <script type="text/javascript">
      alert('This is the content');
    </script>
  </div>

  <script type="text/javascript">
    jQuery('div#div2').appendTo('div#div1');
  </script>

Using this code, alert message will be displayed twice (once when the page is loading, and then when jQuery re-execute the script when it execute appendTo method?

Any idea of how to use jQuery to conveniently move element (that have script tag) around without re-executin the javascript?

Thanks.

I have this code snippet:

  <div id="div1">
  </div>

  <div id="div2">
    <h3>This is the content</h3>
    <script type="text/javascript">
      alert('This is the content');
    </script>
  </div>

  <script type="text/javascript">
    jQuery('div#div2').appendTo('div#div1');
  </script>

Using this code, alert message will be displayed twice (once when the page is loading, and then when jQuery re-execute the script when it execute appendTo method?

Any idea of how to use jQuery to conveniently move element (that have script tag) around without re-executin the javascript?

Thanks.

Share Improve this question asked Jun 25, 2009 at 3:27 NordinNordin 3,2876 gold badges30 silver badges35 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 11

Once the <script> has executed you don't need it any more, so you could remove it before moving #div2.

$('#div2 script').remove();
$('#div2').appendTo('#div1');

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742374108a4431842.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信