Javascript With Quote Causes Missing Argument In Blogger Template

情到浓时终转凉″ 提交于 2019-12-11 17:59:40

问题


When I use the following code, it works fine:

    <script>
      $(document).ready(function() {
        $('#article-sidebar-tabbed-archive-gadget').prepend('<nav></nav>');
      });
    </script>

However, if I use any kind of quotes, I get the following error:

Uncaught SyntaxError: missing ) after argument list

Example of the code used that causes the error:

    <script>
      $(document).ready(function() {
        $('#article-sidebar-tabbed-archive-gadget').prepend('<nav><a href="#">Tags</a></nav>');
      });
    </script>

What causes the error and how can I fix it?


回答1:


Unless the code is inside an HTML/Javascript widget or inside a post, try like this:

    <script>
// <![CDATA[
      $(document).ready(function() {
        $('#article-sidebar-tabbed-archive-gadget').prepend('<nav><a href="#">Tags</a></nav>');
      });
// ]]>
    </script>

The reason is that Blogger has a way to mess some javascript coding unless you wrap it inside the CDATA. A bit of trial an error for me to learn that. Or maybe you just need to escape the all a element like this:

Anyway, I tested you code inside a new blogger post and it works fine. So, if I had to guess, the solutions I mention should work...




回答2:


Maybe you just need to escape the double quotes, try this:

<script>
  $(document).ready(function() {
    $('#article-sidebar-tabbed-archive-gadget').prepend('<nav><a href=\"#\">Tags</a><nav>');
  });
</script>


来源:https://stackoverflow.com/questions/57700489/javascript-with-quote-causes-missing-argument-in-blogger-template

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!