<分区>
<分区>
我想知道是否可以在 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/