JavaScript 动态修改Head 头部样式

9 min read
/**
 * @param {string} documentId
 * @param {string} css 传入css字符串
 */
export const replaceStyle = (documentId, css) => {
  const style = document.getElementById(id);
  try {
    style.innerHTML = css;
  } catch (e) {
    style.styleSheet.cssText = css;
  }
  const head = document.getElementsByTagName("head")[0];
  head.appendChild(style);
};