我有樣式 css cur_font 類我想通過 jquery 將 font-family 更改為 'arial' 以及受類選擇器 html、body、h2、h1、div、td 影響的新字體
<style>
.cur_font html,body,h2,h1,div,td { font-family:'tahoma'; }
</style>
<script>
$(".change_font").on("click", function (e) {
var font='arial';
$("style.cur_font").css("font-family",font);
});
</script>
嘗試使用 jquery 更改類 cur_font 的 font-family 屬性
uj5u.com熱心網友回復:
最簡單的方法是在 body 上切換一個類并應用不同的樣式。
另一種方法是遍歷 document.styleSheets,然后遍歷 cssRules 并檢查 selectorText 是否匹配。如果是,請修改規則。
其他選項是使用 CSS 變數并更改變數的值。
document.querySelector(".buttons").addEventListener("click", evt => {
const btn = evt.target.closest("button");
if (!btn) return;
const color = btn.dataset.color;
document.documentElement.style.setProperty('--myVariable', color);
});
:root {
--myVariable: red;
}
div {
background-color: var(--myVariable);
}
<div>Hello World</div>
<div class="buttons">
<button data-color="blue">blue</button>
<button data-color="red">red</button>
<button data-color="yellow">yellow</button>
</div>
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/527469.html
