如何在IE11上的YandexMaps上更改按钮的类名?

时间:2019-01-24 04:19:30

标签: javascript internet-explorer-11 yandex-maps

我使用以下代码创建自定义按钮:

const clusterShowButton = new ymaps.control.Button({ data: {
    content: SVG_CLUSTER_SHOW,
    title: 'Отключить кластеризацию',
    selectOnClick: true,
    size: 'small'
  }
});

之后,我尝试分配一些CSS类:

clusterShowButton._layout._buttonElement.className += 'my-button';

此功能适用于Internet Explorer 11以外的所有浏览器(即使在Microsoft Edge中)。经过进一步调查,我意识到在IE11中clusterShowButton._layout为空。

问题:

  1. 如何分配自定义课程? (我知道更改以下划线命名的私有属性并不正确)
  2. 是YandexMaps错误吗?

1 个答案:

答案 0 :(得分:0)

您可以参考代码为自定义按钮添加按钮布局,然后添加类:

ymaps.ready(init);

function init() {
    var myMap = new ymaps.Map('map', {
            center: [55.650625, 37.62708],
            zoom: 10,
            controls: []
        }),

    /**
     * The button layout should display the 'data.content' field 
     * and change depending on whether the button was clicked or not.
     * The current size (small, medium, large) is calculated from the value of the 'maxWidth' option
     * @see https://api.yandex.ru/maps/doc/jsapi/2.1/ref/reference/control.Button.xml#param-parameters
      */
        ButtonLayout = ymaps.templateLayoutFactory.createClass([
            '<div title="{{ data.title }}" class="my-button ',
            '{% if state.size == "small" %}my-button_small{% endif %}',
            '{% if state.size == "medium" %}my-button_medium{% endif %}',
            '{% if state.size == "large" %}my-button_large{% endif %}',
            '{% if state.selected %} my-button-selected{% endif %}">',
            '<img class="my-button__img" src="{{ data.image }}" alt="{{ data.title }}">',
            '<span class="my-button__text">{{ data.content }}</span>',
            '</div>'
        ].join('')),

        button = new ymaps.control.Button({
            data: {
                content: "Click-click-click",
                image: 'images/pen.png',
                title: "Click-click-click"
            },
            options: {
                layout: ButtonLayout,
                maxWidth: [170, 190, 220]
            }
        });

    myMap.controls.add(button, {
        right: 5,
        top: 5
    });
}

更多详细信息,请检查Custom button layout