0

我正在开发一个 wordpress 主题,现在我正在处理主题选项页面。我添加了一个带有“选择”方法的选项,让用户可以选择更改滑块的效果。现在......这是一个文本输入的输出,它设置了 fx 持续时间<?php echo get_option('wis_fx_speed'); ?>并且工作正常!我的问题是这<?php echo $fxSample ?>不起作用。尝试了我所知道和想象的一切(语法、顺序、将开关放入 jquery 脚本等),在网上搜索但一无所获……你能帮帮我吗?提前致谢!

<?php 

      switch (get_option('wis_fx_slider')) {
      case "opacity":
      $fxSample = "opacity : 'toggle',"; 
      break;
      case "width":
      $fxSample = "width : 'toggle',"; 
      break;
      case "opawidth":
      $fxSample = "opacity : 'toggle', width : 'toggle',"; 
      break;
      case "blink":
      $fxSample = "opacity : 'show',"; 
      break;
      }

?>


<script type="text/javascript">
    jQuery(document).ready(function($){
        $("#photo-rotator").tabs({fx:{<?php echo $fxSample ?>
        duration: <?php echo get_option('wis_fx_speed'); ?> }}).tabs("rotate", 2000);
    });
</script>

输出:

<

script type="text/javascript">
    jQuery(document).ready(function($){
        $("#photo-rotator").tabs({fx:{        duration: 3000 }}).tabs("rotate", 2000);
    });
</script>

编辑 如果我用收音机制作它就可以了。

4

4 回答 4

1

When I need to pass some data to JavaScript, I find it helpful to build a PHP array first, then use json_encode to convert it to JavaScript. This avoids a lot of potential issues:

<?php 
  $tab_options = array(
    'duration' => get_option('wis_fx_speed'),
    'fx' => array()
  );
  switch (get_option('wis_fx_slider')) {
    case 'opacity':
      $tab_options['fx']['opacity'] = 'toggle';
      break;
    case 'width':
      $tab_options['fx']['width'] = 'toggle';
      break;
    case 'opawidth':
      $tab_options['fx']['opacity'] = 'toggle';
      $tab_options['fx']['width'] = 'toggle';
      break;
    case 'blink':
      $tab_options['fx']['opacity'] = 'show';
      break;
  }
?>

<script type="text/javascript">
  var tabOptions = <?php echo json_encode($tab_options); ?>;
  jQuery(document).ready(function($) {
    $('#photo-rotator').tabs(tabOptions).tabs('rotate', 2000);
  });
</script>

Benefits:

  • Much cleaner code (easier to read).
  • You don't have to worry about getting the commas, quotes, and other syntax right for all the possible cases. For example, as other people have mentioned, you don't have a default case. This approach will at least generate valid JavaScript if an option value is invalid.
于 2012-06-11T21:02:54.683 回答
0

是否get_option('wis_fx_slider')返回开关盒中的选项之一?

您尚未指定“默认值”,因此$fxSample未定义。

<?php 

switch (get_option('wis_fx_slider')) {
    case "opacity":
        $fxSample = "opacity : 'toggle',"; 
        break;
    case "width":
        $fxSample = "width : 'toggle',"; 
        break;
    case "opawidth":
        $fxSample = "opacity : 'toggle', width : 'toggle',"; 
        break;
    default: // blink or anything else
        $fxSample = "opacity : 'show',";
}

?>

您的持续时间是字符串还是数字?如果它是一个字符串,你需要像这样的引号

duration: '<?php echo get_option('wis_fx_speed'); ?>' }}).tabs("rotate", 2000);

查看源代码时会发生什么?PHP 是否正确布置了动态生成的 javascript?你可以发布输出吗?

还有,格式化

<?php 

switch (get_option('wis_fx_slider')) {
    case "opacity":
        $fxSample = "opacity : 'toggle',"; 
        break;
    case "width":
        $fxSample = "width : 'toggle',"; 
        break;
    case "opawidth":
        $fxSample = "opacity : 'toggle', width : 'toggle',"; 
        break;
    case "blink":
        $fxSample = "opacity : 'show',"; 
        break;
}

?>


<script type="text/javascript">
    jQuery(document).ready(function($){
        $("#photo-rotator").tabs({
            fx: {
                <?php echo $fxSample ?>
                duration: <?php echo get_option('wis_fx_speed'); ?>
            }
        }).tabs("rotate", 2000);
    });
</script>
于 2012-06-11T20:51:02.020 回答
0

每个 php 行后都需要一个分号

...
$("#photo-rotator").tabs({fx:{<?php echo $fxSample; ?>
...
  • 默认情况下的值呢?

  • 尝试调试做一个

    var_dump(get_option('wis_fx_slider'));
    var_dump($fxSample);
    
  • 也许这是一个范围问题?尝试定义

    $fxSample = "";
    

    在开始时,在转换之前

于 2012-06-11T20:43:14.740 回答
0

要将任何 PHP 值安全地输出到 JavaScript 值中,请使用json_encode.

...{fx:<?php echo json_encode($fxSample); ?>, duration: <?php echo json_encode(get_option("wis_fx_speed")); ?>}...

另请注意,您的代码中缺少 a,并且有一个额外}的。始终查看生成的结果以查找语法错误。

于 2012-06-11T20:46:17.460 回答