如何以编程方式 expand/collapse 属性?

How to expand/collapse attributions programmatically?

我添加了一个属性作为图例,它可以手动展开或折叠,但是如何以编程方式实现?

找到按钮并点击它。

var fireClick = function(node) {
  if (typeof MouseEvent === 'function') {
    var mevt = new MouseEvent('click', {
      view: win,
      bubbles: false,
      cancelable: true
    });
    node.dispatchEvent(mevt);
  } else if ( doc.createEvent ) {
    // Fallback
    var evt = doc.createEvent('MouseEvents');
    evt.initEvent('click', false, false);
    node.dispatchEvent(evt);
  } else if (doc.createEventObject) {
    node.fireEvent('onclick') ;
  } else if (typeof node.onclick === 'function' ) {
    node.onclick();
  }
};

var map_element = document.getElementById('map');
var button = map_element.querySelector('div.ol-attribution button');

fireClick(button);

ol.control.Attribution 上有 setCollapsiblesetCollapsed 方法:

http://openlayers.org/en/master/examples/attributions.html & http://openlayers.org/en/master/apidoc/ol.control.Attribution.html#setCollapsed