未终止的字符串文字错误

A.C*_*aji 2 html javascript jquery

我已将下面的代码放在标记内.

<script type="text/javascript">
    jQuery(document).ready(function(){
        jQuery('head').append('<script type="text/javascript" src="../wp-includes/js/thickbox.js"></script>');    
    });
</script>
Run Code Online (Sandbox Code Playgroud)

我收到了"未终止字符串文字"的错误.

Tro*_*ter 10

问题来自于您的代码包含</script>在其中的事实.这导致浏览器认为您<script>提前终止了代码.如果您将代码更改为以下代码,它将起作用:

<script type="text/javascript">
    jQuery(document).ready(function(){
        jQuery('head').append('<script type="text/javascript" src="../wp-includes/js/thickbox.js"></' + 'script>');    
    });
</script>
Run Code Online (Sandbox Code Playgroud)

如您所见,上面的代码将</script>您的字符串中断'</' + 'script>',因此浏览器不会将其解析为结束标记.

  • @balaji为什么不使用jquery的getscript,它会照顾你的一切 (2认同)