js读取字符串值为html

时间:2015-12-13 22:37:58

标签: javascript html

我有几个包含HTML元素的字符串,例如:

    SELECT PlayerId, MatchesWon 
    FROM
    (
    SELECT FixtureID,PlayerId, Count(*) As MatchesWon
    FROM Result r 
    WHERE r.Frameswon = (SELECT max(frameswon) FROM Result r2
                  WHERE 
                  r.FixtureId = r2.FixtureId)

   GROUP BY FixtureId,PlayerID
   ) s
   WHERE 
   PlayerID IN (1,3)

然而,我收到一个错误:

$("textarea").change(function(){
        var $html = $("#html").val();
        var $css = $("#css").val();
        var $js = $("#js").val();
        var so = "<style>";
        var sc = "</style>";
        var jso = "<script>window.onload=function(){";
        var jsc = "}</script>";
        var ho = "<body>";
        var hc = "</body>";
        $("iframe#display").html(so + $css + sc + jso + js + jsc + ho + $html + hc);
});

我正在使用jQuery Unexpected token ILLEGAL 函数来插入内容,但错误正在逐渐显现出来。为什么我会收到此错误?

1 个答案:

答案 0 :(得分:1)

</script>将终止脚本元素。如果你把它放在一个字符串的中间,它会在那里终止它。

将您的脚本移至外部.js文件或将字符序列表示为<\/script>

screenshot