开发者

Dealing with radio buttons in external Javascript

开发者 https://www.devze.com 2023-03-03 15:07 出处:网络
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:

I have a page with three radio buttons, as such:

Dealing with radio buttons in external Javascript

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消