-1

我有一个包含一些 html 和大量 Javascript 的 div

<div id="mydiv">
<p>Hello Philippines</p> my first time in Philippines is nice
<script type="text/javascript">alert("how was it became nice?");</script>
well I experienced a lot of things
<script type="text/javascript">alert("and how about your Japan's trip?");</script>
well its more nicer ^^ but both countries are good! hahah
</div>

我想将非 Javascript 代码和 Javascript 代码放在两个单独的变量中:

var html = $("#mydiv").html();

我的问题是我的 javascript 正在执行,这让我停止创建我想要的代码,即将 javascript 和非 javascript 存储到两个不同的变量。

  1. 当javascript代码进入div时,如何阻止它们执行?
  2. 如何安全地将 javascript 和非 javascript 代码存储到两个不同的变量中?

注意:我需要存储的 javascript 以供以后执行

4

1 回答 1

2

您可以使用code标签代替script

html:

<div id="mydiv">
<p>Hello Philippines</p> my first time in Philippines is nice</p>
<code>alert("how was it became nice?");</code>
well i experienced a lot of things
<code>alert("and how about your Japan's trip?");</code>
<p>well its more nicer ^^ but both countries are good! hahah
</p>
</div>

js:

var html = $('#mydiv *').not('code').html()
var code = $('#mydiv code').text()

http://jsfiddle.net/dMDy5/3/

于 2012-07-07T09:14:13.027 回答