我使用这种语法通过jquery在我的视图上编写一个字符串:
$(".temp_class").append('<p>my name</p>')
它显示:我的名字但是当我使用它时:
$(".temp_class").append('<script type="text/javascript">alert("s");</script>')
它会弹出一个警告对话框,如何处理它,将其写成字符串而非HTML标记,以防止出现警告对话框。
我不想写HTML标签,我想把它显示为字符串
答案 0 :(得分:1)
为简单起见,您可以使用<pre><code>
标记封装代码字符串。您可以使用下面的代码美化插件。
CSS/JS to format and display code samples on a website
http://google-code-prettify.googlecode.com/svn/trunk/README.html
答案 1 :(得分:1)
你也可以试试这个
var str= "<script>alert('hi!');</script>";
var escaped = $(".temp_class").text(str).html();
这意味着你需要这样做,使用原型函数
var script = '<script type="text/javascript">alert("s");</script>'.escapeHTML();
$(".temp_class").append(script);
检查原型:http://www.tutorialspoint.com/prototype/prototype_string_escapehtml.htm
你需要像这样正确地格式化你的字符串,所以这将显示<b>Availability</b>
$mytext = "<b>Availability:</b>"
$(".temp_class").append($mytext);
答案 2 :(得分:0)
首先,你错过了开放的<
。
但是你只需要在触发后显示警告。您不必将警报放在班级内。
答案 3 :(得分:0)
您可以将代码括在<pre></pre>
在你的情况下是:
$(".temp_class").append('<pre><script type="text/javascript">alert("s");</script></pre>');
答案 4 :(得分:0)
你可以试试这个:
$(".temp_class").append('<script type="text/javascript"><\/script>');
$(".temp_class").find('script').append('alert("s");');
由于这是动态完成的,你必须使用inspector / firebug在源代码中查看它。
或者如果你想把它作为一个字符串,那么.text()
就会这样做:
$(".temp_class").text('<script type="text/javascript">alert("s");<\/script>');