0

我正在尝试将 html 放入 javascript 代码中:

       document.write("<a class=\"twitter-timeline\"href=\"https://twitter.com/test\"          data-widget-    id=\"248798161076883457\">Tweets by @test</a>
             <script>!function(d,s,id){
              var js,fjs=d.getElementsByTagName(s)[0];
             if(!d.getElementById(id))
              {js=d.createElement(s);
          js.id=id;js.src=\"//platform.twitter.com/widgets.js\";
              fjs.parentNode.insertBefore(js,fjs);
        }
       }(document,\"script\",\"twitter-wjs\");
       </script>
        "); 

}

错误信息是:Unexpected token ILLEGAL 怎么了?谢谢!

4

2 回答 2

4

javascript 中的字符串在没有正确格式的情况下不得超过多行:

   document.write("<a class=\"twitter-timeline\"href=\"https://twitter.com  /test\"          data-widget-    id=\"248798161076883457\">Tweets by @test</a>\
         <script>!function(d,s,id){\
          var js,fjs=d.getElementsByTagName(s)[0];\
         if(!d.getElementById(id))\
          {js=d.createElement(s);\
      js.id=id;js.src=\"//platform.twitter.com/widgets.js\";\
          fjs.parentNode.insertBefore(js,fjs);\
    }\
   }(document,\"script\",\"twitter-wjs\");\
   </script>\
    "); 

一般来说:

string = "Multiple\
Lines\
supported."
于 2012-09-20T15:30:01.657 回答
0
document.write("<a class='twitter-timeline' href='https://twitter.com/test'  data-widget- id='248798161076883457'>Tweets by @test</a><script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src='platform.twitter.com/widgets.js'; fjs.parentNode.insertBefore(js,fjs);}}(document,'script','twitter-wjs');</script>"); ​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​

您可以在脚本中使用单引号而不是双引号!它使阅读更容易,而且效果很好

于 2012-09-20T15:30:57.320 回答