简体   繁体   English

Wordpress帖子上的Javascript

[英]Javascript on wordpress posts

I've been trying to use javascript on my wordpress post for over 2 hours now. 我已经尝试在我的wordpress帖子上使用javascript超过2个小时了。 I researched everything there is to research, and it still isn't working. 我研究了要研究的所有内容,但仍然没有用。

I've made sure to paste my code in the "text" tab of wordpress. 我已经确保将代码粘贴到wordpress的“文本”标签中。

Can I get some help? 我可以帮忙吗?

Here's my code: 这是我的代码:

<script language="Javascript">
<!-- 
              // Array of day names
              var dayNames = new Array("Sunday","Monday","Tuesday","Wednesday",
                  "Thursday","Friday","Saturday");

              // Array of month Names
              var monthNames = new Array(
              "January","February","March","April","May","June","July",
              "August","September","October","November","December");

              var now = new Date();
              document.write(dayNames[now.getDay()] + ", " + 
              monthNames[now.getMonth()] + " " + 
              now.getDate() + ", " + now.getFullYear());

              // -->
</script>

Get rid of all the white spaces and line breaks in your script. 删除脚本中的所有空白和换行符。 The WordPress is appending p tags to your code because of white spaces and line breaks and your code looks like this on execution: WordPress由于空格和换行符而在代码中附加了p tags ,并且代码在执行时如下所示:

<script language="Javascript">
<!-- 
              // Array of day names
              var dayNames = new Array("Sunday","Monday","Tuesday","Wednesday",
                  "Thursday","Friday","Saturday");</p>
<p>              // Array of month Names
              var monthNames = new Array(
              "January","February","March","April","May","June","July",
              "August","September","October","November","December");</p>
<p>              var now = new Date();
              document.write(dayNames[now.getDay()] + ", " + 
              monthNames[now.getMonth()] + " " + 
              now.getDate() + ", " + now.getFullYear());</p>
<p>              // -->
</script>

Try this: 尝试这个:

<script type="text/javascript">
<!--
  // Array of day names
  var dayNames = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
  // Array of month Names
  var monthNames = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
  var now = new Date();
  document.write(dayNames[now.getDay()] + ", " + 
  monthNames[now.getMonth()] + " " + 
  now.getDate() + ", " + now.getFullYear());
// -->
</script>

There are plenty of Wordpress plugins that provide that in a shortcode. 有许多Wordpress插件以短代码提供该功能。 For example: http://wordpress.org/plugins/extra-shortcodes , this also a lot less cumbersome than including a script every time you post. 例如: http : //wordpress.org/plugins/extra-shortcodes ,这也比每次发布时都包含脚本要麻烦得多。

what wp verion are you running. 您正在运行什么wp版本。 in wp 3.6.1, if you add js code in post editor, wp will add <p> tags around things in script. 在wp 3.6.1中,如果在帖子编辑器中添加js代码,wp会在脚本中的内容周围添加<p>标记。 if you remove the comments <!-- blah --> , wp adds cdata around the script: 如果删除注释<!-- blah --> ,wp将在脚本周围添加cdata:

<script type="text/javascript">
//<![CDATA[
...code...
//]]>
</script>

but in frontend output it changes the last part of cdata //]]> to // ]]&gt; 但是在前端输出中,它将cdata的最后部分//]]>更改为// ]]&gt;

i cant offer a solution as this would require an external js file. 我无法提供解决方案,因为这需要外部js文件。 that being said, move js code to external file and all will be rosy 话虽如此,将js代码移动到外部文件中,一切都会很顺利

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM