php - 在 onchange 事件中更新 css

标签 php html css events session

<分区>

我想知道是否可以在 onchange 事件上编辑 css 属性。

我的 HTML + 脚本

    <div class="Content">

        <input id="ColorSlider" type="range" min="0" max="360" value="25" onchange="showValue(this.value)"/>
        <div id="ColorViewer">
            Color value = 25
        </div>

        <script>
            function showValue(newValue){
                document.getElementById("ColorViewer").innerHTML= "Color value = " + newValue;
            //something here that sends the value to the css file

}

还有我的 CSS,它现在从另一个 PHP 文件接收 session 变量

<?php
header('Content-type: text/css');
    session_start();

    $CssBorder = $_SESSION['CssBorder'];
    $CssH1BackgroundColor = $_SESSION['CssH1BackgroundColor'];

?>

是否可以将变量(可能通过 PHP session )发送到 CSS 文件。并更改 HTML 边框的颜色?

为了清除目的,我想要我的 css 文件。

最佳答案

您真的不需要为此使用 PHP。这可以使用 javascript 轻松完成。

<script>
function showValue(newValue){
    document.getElementById("ColorViewer").innerHTML= "Color value = " + newValue;
    //something here that sends the value to the css file
    document.getElementById("ColorViewer").style.border = "[width] [style] [colour]";
}
</script>

但是,如果您绝对必须动态加载生成的外部样式表,那么以下使用 jQuery 的链接可能对您有用:

How to load up CSS files using Javascript?

此外,您的 PHP 生成的 CSS 文件可能会使用 $_GET[] 变量而不是 $_SESSION[] 变量响应更好,因为您将无法设置来自用户端 HTML 的 session 变量。

关于php - 在 onchange 事件中更新 css,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20631619/

上一篇:css - 如何覆盖特定控件的 css?

下一篇:php - 如何防止在 php html 中包装 div

相关文章:

php - laravel 取消数据库查询

php - DOM标签和属性规则和过滤

html - 按钮中的文字不在一行

html - Shiny - 将下拉菜单(选择标签)的大小(填充?)更改得更小

html - 背景重复不适用于移动设备

PhpStorm、PHPUnit 和 setcookie

html - 调整窗口大小时,父背景 DIV 被截断

html - 我无法使 Bootstrap 表单响应

css - 宽度 : 100PC vs media queries

php - 如何将 PHP 数组格式化为字符串