按钮计数器的短代码

时间:2014-12-14 17:43:10

标签: javascript php wordpress button

我只是想创建一个短代码来计算按下按钮的次数,我在function.php文件中有这个代码。

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');

但是我收到了以下错误。

  

解析错误:语法错误,意外&#39; '<script type="text/javascript&#39;第4645行/home1/election/public_html/wp-content/themes/Divi/functions.php中的(T_CONSTANT_ENCAPSED_STRING)

感谢任何帮助。 提前致谢

1 个答案:

答案 0 :(得分:0)

尝试一下:

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');