Javascript 根据cookie的存在高亮显示按钮

Javascript 根据cookie的存在高亮显示按钮,javascript,jquery,html,css,cookies,Javascript,Jquery,Html,Css,Cookies,我的网站上有几个按钮和几个cookie。我有一个名为chosenValue的cookie,我想突出显示与存储在cookie中的值对应的值。例如,我检索存储在cookie中的值: var value = readCookie('chosenValue'); 假设存储的值是id1。然后,我想突出显示与id1关联的按钮 <input type="button" class="buttons" name="button-terms" value="id1"></input> &

我的网站上有几个按钮和几个cookie。我有一个名为chosenValue的cookie,我想突出显示与存储在cookie中的值对应的值。例如,我检索存储在cookie中的值:

var value = readCookie('chosenValue');
假设存储的值是id1。然后,我想突出显示与id1关联的按钮

<input type="button" class="buttons" name="button-terms" value="id1"></input>
<input type="button" class="buttons" name="button-terms" value="id2"></input>
<input type="button" class="buttons" name="button-terms" value="id3"></input></h3>
我该怎么做

试试看:

JS:

CSS:


将.buttons:hover更改为.buttons:hover,.button在CSS中处于活动状态,然后在javascript中:

$(".buttons[value="+readCookie('chosenValue')+"]").addClass("button-active");

给他们一个与值对应的id。然后通过document.getElementByIdreadCookie'chosenValue获取元素,然后可以为元素使用attribute.className`。此cookie的值是否会更改?
document.getElementById(readCookie('chosenValue')).className = 'buttons highlighted';
.buttons.highlighted { background: orange }
$(".buttons[value="+readCookie('chosenValue')+"]").addClass("button-active");