简体   繁体   English

Wordpress 自定义插件后端复选框在前端更改 css

[英]Wordpress custom plugin backend checkbox changing css in frontend

I am developing a custom plugin and I am pretty much done with it, my only problem right now is taking data from a script that is enqueued both in the Admin and frontend but it does not work as I thought it would.我正在开发一个自定义插件,我已经完成了它,我现在唯一的问题是从一个在管理和前端排队的脚本中获取数据,但它不像我想象的那样工作。 So, I have created a couple of checkboxes in the admin panel of the plugin and I want when someone clicks one of the checkboxes to change CSS from a div to make it look like the example image above the checkbox.因此,我在插件的管理面板中创建了几个复选框,当有人单击其中一个复选框时,我希望将 CSS 从 div 更改为看起来像复选框上方的示例图像。

my code:我的代码:

function vsuc_page_html() { ?>
<div class="wrap vsuc-wrapper">
    <form method="post" action="options.php">
        <?php settings_errors() ?>
        <?php settings_fields('vsuc_option_group'); ?>
  <?php echo '<h3 class="header_admin">Viorel Stanciu User Counter Plugin</h3>'; ?>
        <label class="label_text" for="vsuc_field_eat">Text Label:</label>
        <input name="vsuc_text_field" id="vsuc_field_eat" type="text" value=" <?php echo get_option('vsuc_text_field'); ?> "><br>
    <input type="checkbox" id="vsuc_checkbox1" name="vsuc_checkbox1" value="style1">
    <label for="checkbox1"><img src="<?php echo plugin_dir_url( __FILE__ ); ?>/img/style1.PNG" class="style1"></label>
    <input type="checkbox" id="checkbox2" name="vsuc_checkbox2" value="style2">
    <label for="checkbox2"><img src="<?php echo plugin_dir_url( __FILE__ ); ?>/img/style2.png" class="style1"></label>
        <?php echo '<p class="shortcode"> Insert this shortcode where you want to display the plugin: [pagehits]';
     submit_button(); ?>
    </form>
</div>

function vsuc_register_settings() {
    register_setting('vsuc_option_group', 'vsuc_text_field');
    register_setting('vsuc_option_group', 'vsuc_checkbox1');
}

The checkboxes appear correctly, but they are not doing anything.复选框正确显示,但它们没有做任何事情。

Test script:测试脚本:

$(document).ready(function () {
    $('#vsuc_checkbox1').change(function () {
        var x = document.getElementsByClassName("visitor_count");
        if (this.checked) {
            x.style.display = "none !important";
        } else {
            x.style.display = "block !important";
        }
    })
});

Any help or hint would be much appreciated.任何帮助或提示将不胜感激。

It appears that you are using jQuery.看来您正在使用 jQuery。 If your goal is to keep it in your project.如果您的目标是将其保留在您的项目中。 I suggest to use some of its methods in order to tidy up your code.我建议使用它的一些方法来整理你的代码。

I don't know how familiar you are with javascript but when you use getElementsByClassName() the return value is going to be a collection of javascript objects.我不知道您对 javascript 有多熟悉,但是当您使用getElementsByClassName()时,返回值将是 javascript 对象的集合。 This means that in order to target any of your .visitor_count and access it's style attribute you need to target one of the object in the collection.这意味着,为了定位您的任何.visitor_count并访问它的样式属性,您需要定位集合中的 object 之一。 If you only have one .visitor_count you may as well use x[0] to target the first element in the collection since it's the only one.如果您只有一个.visitor_count您也可以使用x[0]来定位集合中的第一个元素,因为它是唯一的一个。

The jQuery way of doing this would look like this:执行此操作的 jQuery 方式如下所示:

$(function () {
   $("#vsuc_checkbox1").change(function () {
      let x = $(".visitor_count");
      if (this.checked) {
         x.hide();
      } else {
         x.show();
      }
   });
});

Personal tip, starting a variable name with a dollar sign when it is containing a jQuery object (for example using let $x instead of let x ) is a great way to tell when to use jQuery methods on a variable instead of plain javascript. Personal tip, starting a variable name with a dollar sign when it is containing a jQuery object (for example using let $x instead of let x ) is a great way to tell when to use jQuery methods on a variable instead of plain javascript.

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

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