附加具有多个单引号的代码

时间:2015-12-16 08:24:33

标签: javascript jquery

我必须在body附加下面的代码,但当我这样做onReady时,我收到此错误:

  

未终止的字符串文字

下面是我附加的代码:

jQuery('body').append('
    <!--
        Start of DoubleClick Floodlight Tag: Please do not remove
        This tag must be placed between the <body> and </body> tags, as close as possible to the opening tag.
        Creation Date: 10/12/2015
    -->
        <script type="text/javascript">
        var axel = Math.random() + "";
        var a = axel * 10000000000000;
        document.write(\'<iframe src="https://338333293.fls.doubleclick.net/activityi\' + a + \'?" width="1" height="1" frameborder="0" style="display:none"></iframe>');
        </script>
        <noscript>
        <iframe src="https://333383293.fls.doubleclick.net/activityi;src=3383293;?" width="1" height="1" frameborder="0" style="display:none"></iframe>
        </noscript>
    <!-- End of DoubleClick  -->
');

我已经完成了这个StackOverflow Suggestion

但这对我不起作用

3 个答案:

答案 0 :(得分:1)

对于这个,有多种方式:

<强>首先

"Hello World"
+"Yo it's amazing"
+'Yes I "Love" it';

第二:使用``(模板)

Hello World Yo it's amazing Yes I "Love" it

答案 1 :(得分:0)

您的代码应为

//Create Html with proper concatenation 
var html='<!--Start of DoubleClick Floodlight Tag: Please do not remove This tag must be placed between the <body> and </body> tags, as close as possible to the opening tag. Creation Date: 10/12/2015-->';
html+='<script type="text/javascript">';
html+='                    var axel = Math.random() + "";';
html+='var a = axel * 10000000000000;';
html+='document.write(\'<iframe src="https://338333293.fls.doubleclick.net/activityi\' + a + \'?" width="1" height="1" frameborder="0" style="display:none"></iframe>\');';
html+='</script>';
html+='<noscript>';
html+='<iframe src="https://333383293.fls.doubleclick.net/activityi;src=3383293;?" width="1" height="1" frameborder="0" style="display:none"></iframe>';
html+='</noscript>';
html+='<!-- End of DoubleClick  -->';

//append html to dom
jQuery('body').append(html);

答案 2 :(得分:0)

\之后您错过了</iframe>  使用它,看看

<script>
 jQuery('body').append('
                <!--
                Start of DoubleClick Floodlight Tag: Please do not remove
                This tag must be placed between the <body> and </body> tags, as close as possible to the opening tag.
                Creation Date: 10/12/2015
                -->
                <script type="text/javascript">
                var axel = Math.random() + "";
                var a = axel * 10000000000000;
                document.write(\'<iframe src="https://338333293.fls.doubleclick.net/activityi\' + a + \'?" width="1" height="1" frameborder="0" style="display:none"></iframe>\');
                </script>
                <noscript>
                <iframe src="https://333383293.fls.doubleclick.net/activityi;src=3383293;?" width="1" height="1" frameborder="0" style="display:none"></iframe>
                </noscript>
               <!-- End of DoubleClick  -->');
</script>