如何为一个id使用一个脚本?

时间:2014-03-14 20:17:20

标签: javascript jquery map

我有这个代码,我需要使用很多id,而不仅仅是像map1,map1,map2,map3等等。例如($ usamap = $('#map_id' );)如何做到这一点?

$usamap = $('#map1');
    function bindlinks() {
        $('*').unbind();
        $("#unbind_link").bind("click", function (e) {
            e.preventDefault();
            $usamap.mapster("unbind");
            $usamap.width(720);
            bindlinks();
        });
        $("#rebind_link").bind("click", function (e) {
            e.preventDefault();
            $usamap.mapster(default_options);
        });

        $("#unbind_link_preserve").bind("click", function (e) {
            e.preventDefault();
            $usamap.mapster("unbind", true);
            bindlinks();
        });
        $("#tooltip").bind("click", function (e) {
            e.preventDefault();
            var state = !$usamap.mapster('get_options').showToolTip;
            $('#tooltip_state').text(state ? "enabled" : "disabled");
            $usamap.mapster("set_options", { showToolTip: state });
        });
        $("#single_select").bind("click", function (e) {
            e.preventDefault();
            var state = !$usamap.mapster('get_options').singleSelect;
            $('#single_select_state').text(state ? "enabled" : "disabled");
            $usamap.mapster("set_options", { singleSelect: state });
        });
        $("#is_deselectable").bind("click", function (e) {
            e.preventDefault();
            var state = !$usamap.mapster('get_options').isDeselectable;
            $('#is_deselectable_state').text(state ? "enabled" : "disabled");
            $usamap.mapster("set_options", { isDeselectable: state });
        });

1 个答案:

答案 0 :(得分:0)

您可以将地图对象传递到bindlinks。我所做的假设是你有多个地图但只有一个控件,并且你想让控件每次都适用于不同的地图。

function bindlinks(map) {
    // This is really bad..., affecting all links on the page
    // You should only unbind handlers you bound      
    $('*').unbind();

    $("#unbind_link").bind("click", function (e) {
        e.preventDefault();
        map.mapster("unbind");
        map.width(720);
        bindlinks();
    });
    $("#rebind_link").bind("click", function (e) {
        e.preventDefault();
        map.mapster(default_options);
    });

    $("#unbind_link_preserve").bind("click", function (e) {
        e.preventDefault();
        map.mapster("unbind", true);
        bindlinks();
    });
    $("#tooltip").bind("click", function (e) {
        e.preventDefault();
        var state = !map.mapster('get_options').showToolTip;
        $('#tooltip_state').text(state ? "enabled" : "disabled");
        map.mapster("set_options", { showToolTip: state });
    });
    $("#single_select").bind("click", function (e) {
        e.preventDefault();
        var state = !map.mapster('get_options').singleSelect;
        $('#single_select_state').text(state ? "enabled" : "disabled");
        map.mapster("set_options", { singleSelect: state });
    });
    $("#is_deselectable").bind("click", function (e) {
        e.preventDefault();
        var state = !map.mapster('get_options').isDeselectable;
        $('#is_deselectable_state').text(state ? "enabled" : "disabled");
        map.mapster("set_options", { isDeselectable: state });
    });
}

// Call it like
bindlinks( $('#map_id') );

// And later 
bindlinks( $('#map_other_id') )