Javascript记住组合框值
Javascript remember combobox value
本文关键字:组合 Javascript 更新时间:2023-09-26
我目前有3个组合框,其中包含使用javascript将样式应用于页面的值。
所有这些样式都应用于cookie并在主体负载时读取,但是我也想设置组合框的值以匹配应用的样式。
如何根据cookie或应用样式设置组合框值?
<select name="sizes" onchange="fontSize(accessibility.sizes.value);">
<option value='-1'>Select</option>
<option value='10'>Small</option> <option value='12.75'>Medium</option>
<option value='15.5'>Large</option>
</select>
function fontSize(textSize) {
var element = document.getElementById('content');
if (element) {
element.style.fontSize = textSize + 'px';
createCookie("Font Size", textSize, 365);
}
}
提前感谢!
如果更改select就是更改样式,请首先选择相关值,然后触发onchange。请注意,我更改了您的脚本以保存selectedIndex,并在设置索引后应用更改。还请注意,我通过了(这个),选择对象本身,我已经添加了一个ID到它。最后,我把中等字体大小作为第一个项目,以防人们想要重置。请将其更改为默认的字体大小
window.onload=function() {
var sel = document.getElementById('sizes');// get the select with ID=sizes
// the following statement gets the select and sets the index to the cookie content if
// there IS a cookie, else it sets it to 0
var idx = getCookie("FontSizeIdx") || 0;
sel.selectedIndex = parseInt(idx,10); // make sure it is a number
// now call the function and pass the select to it
fontSize(sel);
}
function fontSize(sel) { // select object is passed as (this) and ends up here as (sel)
var textSize = sel.options[sel.selectedIndex].value; // get the value
var element = document.getElementById('content'); // get the content object
if(element) { // does it exist?
element.style.fontSize = textSize + 'px'; // set the font size
createCookie("FontSizeIdx", sel.selectedIndex, 365); // save the index
}
}
<select name="sizes" id="sizes" onchange="fontSize(this);">
<option value='12.75'>Select (default is medium)</option>
<option value='10'>Small</option>
<option value='12.75'>Medium</option>
<option value='15.5'>Large</option>
</select>
相关文章:
- 在单独加载时运行良好的组合javascript中可能导致错误的原因
- 组合 JavaScript 数组
- 组合 Javascript 函数
- 如何组合 JavaScript 对象
- 从选择/组合 JavaScript 中删除节点
- 组合 JavaScript 函数
- 组合 Javascript 变量
- 如何在 HTML 中组合 JavaScript 变量
- 函数组合javascript
- 如何缩小和组合Javascript和CSS
- 编程岩石,纸张,剪刀的选择组合-Javascript
- 比较和组合Javascript中的数组
- AngularJS通过组合javascript文件减少HTTP请求
- 有条件地组合javascript文件作为预编译步骤(比r.js更灵活)
- 在博客上组合Javascript
- 动态组合JavaScript数组
- 创建所有可能的组合- javascript
- 文本框&选择字段组合(Javascript + Rails)
- 使用Javascript预处理器组合Javascript文件
- 哪个更好:公共JavaScriptCDN或自托管组合JavaScript