doujing8435 2014-12-14 17:43
浏览 122
已采纳

按钮计数器的短代码

I just wanted to create a shortcode to count the number of times a button is pressed and i have this code in my function.php file.

function btn_cntr_shortcode(){

'<input type="button" value="Count" id="countButton" />

<p>The button was pressed <span id="displayCount">0</span> times.</p>'

'<script type="text/javascript">'
  var $count = 0;
  var button = document.getElementById("countButton");
  var display = document.getElementById("displayCount");

  button.onclick = function(){
    $count++;
    display.innerHTML = $count;
  }
'</script>'
return $count;

}
add_shortcode('button-counter','btn_cntr_shortcode');

But am getting the following error.

Parse error: syntax error, unexpected ''<script type="text/javascript' (T_CONSTANT_ENCAPSED_STRING) in /home1/election/public_html/wp-content/themes/Divi/functions.php on line 4645

Any help is appreciated. Thanks in advance

  • 写回答

1条回答 默认 最新

  • doubangzhang6992 2014-12-14 19:22
    关注

    Give this a try:

    function btn_cntr_shortcode(){
    
    $output= '<input type="button" value="Count" id="countButton" />
              <p>The button was pressed <span id="displayCount">0</span> times.</p>
    
              <script type="text/javascript">
                var count = 0;
                var button = document.getElementById("countButton");
                var display = document.getElementById("displayCount");
    
               button.onclick = function(){
                 count++;
                 display.innerHTML = count;
                 }
              </script>';
    
    return $output;
    
    }
    add_shortcode('button-counter','btn_cntr_shortcode');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码