栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

JavaScript 如何使用.css()应用!important?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JavaScript 如何使用.css()应用!important?

这些答案大多数现在已经过时,IE7支持不再是问题。

支持IE11 +和所有现代浏览器 的最佳方法是:

const $elem = $("#elem");$elem[0].style.setProperty('width', '100px', 'important');

或者,如果需要,您可以创建一个小型jQuery插件来执行此操作。该插件

css()
在其支持的参数中紧密匹配jQuery自己的方法:

   jQuery.fn.cssimportant = function(name, value) {  const $this = this;  const applyStyles = (n, v) => {    // Convert style name from camelCase to dashed-case.    const dashedName = n.replace(/(.)([A-Z])(.)/g, (str, m1, upper, m2) => {      return m1 + "-" + upper.toLowerCase() + m2;    });     // Loop over each element in the selector and set the styles.    $this.each(function(){      this.style.setProperty(dashedName, v, 'important');    });  };  // If called with the first parameter that is an object,  // Loop over the entries in the object and apply those styles.   if(jQuery.isPlainObject(name)){    for(const [n, v] of Object.entries(name)){       applyStyles(n, v);    }  } else {    // Otherwise called with style name and value.    applyStyles(name, value);  }  // This is required for making jQuery plugin calls chainable.  return $this;};// Call the new plugin:$('#elem').cssimportant('height', '100px');// Call with an object and camelCased style names:$('#another').cssimportant({backgroundColor: 'salmon', display: 'block'});// Call on multiple items:$('.item, #foo, #bar').cssimportant('color', 'red');


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/432152.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号