I have a page with three radio buttons, as such:
I want to make it so when one of these buttons is clicked, the font size of an element changes as such:
$("viewer")开发者_Go百科.style.fontSize = "5pt";
It's fairly straightforward to do this in Javascript with three separate functions. Something like the following should work, although I haven't tested it:
window.onload = function() {
$("small").onclick = small;
$("medium").onclick = medium;
}
function small(){
$("viewer").style.fontSize = "5pt";
}
function medium(){
$("viewer").style.fontSize = "15pt";
}
Is there any way to merge these into one larger function?
<input type="radio" name="size" value="small">Small
<input type="radio" name="size" value="medium">Medium
<input type="radio" name="size" value="large">Large
<script>
$('input[name=size]').click(function()
{
var clickedVal = $(this).val();
var size = 5;
switch (clickedVal)
{
case 'small':
size = 5;
break;
case 'medium':
size = 10;
break;
case 'large':
size = 15;
break;
}
$('#viewer').css('fontSize', size + 'pt');
}
</script>
function changeSize(size, elem){
elem.css('font-size', size);
}
<input type="radio" name="size" value="5">Small
<input type="radio" name="size" value="15">Medium
<input type="radio" name="size" value="25">Large
<input type="radio" name="size" value="35">Super Big
<script>
$('input[name=size]').click(function() {
$('#viewer').css('fontSize', $(this).val() + 'pt');
}
</script>
You can add new radio buttons for more sizes without needing to change the script.
精彩评论