所以我試圖讓我的樣式表在用戶選擇表單中的下拉框到另一個時更改。更改窗體選擇上的樣式表onChange =「change()」
我的代碼如下。
$styles = array(
"style",
"dark"
);
$style = $_POST["style"];
if (!in_array($style, $styles))
{
//we default back to the first in the array, this prevents accidental hardcoding;
$style = $styles[0];
}
$css = "/templates/streamer/css/$style.css";
if (!file_exists($css)){
//something terrible happend;
#echo "missing style $css";
}
function showOptions() {
echo '<form id="selectStyle" action="#" method="post">';
echo '<select id="style" onChange="change()">';
global $styles;
foreach ($styles as &$value){
echo '<option value="'.$value.'">'.$value.'</option>';
}
echo '</select></form>';
}
然後我的link rel如下:
<link rel="stylesheet" href="<?php echo $css; ?>"/>
然後我打電話jQuery腳本:
function change() {
document.getElementById("selectStyle").submit();
}
然後我調用該函數來顯示錶單:
<?php showOptions(); echo $css; ?>
所以我的問題是,一旦我選擇「黑暗」的CSS文件,頁面會刷新並不會更改樣式表。
我也嘗試了許多其他的替代方法,他們也不工作。但是我寫的這種方法似乎是適合我需求的更好選擇。
感謝
我需要改變樣式表,你說的選項和方法是不是我所需要的。表單提交後頁面刷新雖然 – DreamRJ
感謝?或事件'onChange'後? – hassan
從你的PHP中分離你的HTML,當你關閉PHP標籤時不需要'回顯'它。這樣就更容易閱讀和理解。 – Script47