jquery为css函数添加新属性

时间:2016-06-22 20:46:52

标签: jquery css properties

有没有办法在jquery中为css添加新属性名? 像过境这样的东西,有很多新的属性(持续时间,完整等) $ o我需要这样的:

var css_class ={
  class1 : {height:100,background:'#000'},
  //class2 ... etc
}
var my_property = function(value){
  $(this).css(css_class.value);
}

$('#element').css({
  width: '100px',
  my_property: 'class1'
});
//expected ... #element have width 100 also height and backround from class1
//I know it can be solved with a plugin like :
// .css(...).my_plpugin('my_property') but I dont want this 

谢谢。

编辑:因为更多的人会回答我的尝试吗?我在示例中添加了更多详细信息。 也许mi示例不正确或者可以通过其他方式解决(毫无疑问),但最明显的问题是:转换插件(例如)还有其他属性吗? (除了css之外)这样做的机制(方式)是什么? 谢谢。

1 个答案:

答案 0 :(得分:0)

最后我添加了一个jquery源的条件不是专业的解决方案,但它有效:

//I found this line in jquery source
//style: function( elem, name, value, extra ) {

//I added here mi condition
if(name[0] == '.'){
  var my_class_name = name.substr(1);
  $(elem).css(css_class[my_class_name]);
}

所以现在在mi js代码中我可以这样写:

$('#element').css({
  width: '100px',
  '.class1': '', //the empty value for not throw errors 
  //... more css here ...
});
//now the #element have width 100px, height 100px and a black background