开发者

Creating a function for a Javascript and jQuery function

开发者 https://www.devze.com 2023-02-28 15:02 出处:网络
So I have these functions inside of the (document).ready function: $(document).ready(function () { $(\"#border_button\").click(function () {

So I have these functions inside of the (document).ready function:

$(document).ready(function () {
    $("#border_button").click(function () {
        var value = $("#border-radius").attr("value");
        $("div.editable").click(function (e) {
            e.stopPropagation();
            showUser(value, '2', this.id)
            $(this).css({
                "-webkit-borde开发者_StackOverflowr-radius": value
            });
        });
    });
});

I want to be able to write a function with parameters that would be:

(#border_button,#border_radius,2,-webkit-border-radius)

But every time that I write the function and execute it functionName(parameters), I get the error "Undefined"

Does anyone know how to write a function for this that won't return undefined?


You are not creating a function which is callable manually anywhere.

If you are talking about a completely different function: You need to pass your arguments as strings, i.e. quoted with ' or ":

someFunction('#border_button', '#border_radius', 2, '-webkit-border-radius');

To make your current code callable as a function:

function fooBar(selector, fieldSelector, num, cssRule) {
    $(selector).click(function(e) {
        var value = $(fieldSelector).val();
        $('div.editable').click(function(e) {
            e.stopPropagation();
            showUser(value, num, this.id)
            $(this).css(cssRule, value);
        });
    });
}

$(document).ready(function() {
    fooBar('#border_button', '#border_radius', 2, '-webkit-border-radius');
});

Note that I've changed .attr('value') to .val() as that's the jQuery way to get the value of a form field.


Maybe something like this? Not sure why you want to do that though.

$(document).ready(function(){

    function Fos(buttonSel, inputSel, someValue, cssProperty) {
        $(buttonSel).click(function(){   
            var value = $(inputSel).attr("value");
            $("div.editable").click(function (e) { 

                e.stopPropagation();

                showUser(value, someValue, this.id)
                var css_obj={};
                css_obj[cssProperty]=value;
                $(this).css(css_obj);  
            });        
        });        
    }

    Fos('#border_button', '#border-radius', 2, '-webkit-border-radius');
});        
0

精彩评论

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