简体   繁体   English

如何使用jQuery动态更改css的属性

[英]how to dynamically change the property of css using jquery

I am trying to style the DIV using javascript. 我正在尝试使用JavaScript设置DIV的样式。 If the user clicks the DIV, it will dynamically change the value of the property passed after onclick. 如果用户单击DIV,它将动态更改onclick之后传递的属性的值。 I want to try this in order to save my code that's why I chose jquery css. 我想尝试一下以保存我的代码,这就是为什么我选择了jQuery CSS。 But, the property is not working. 但是,该属性不起作用。 Is this possible or is there any other way to make it happen? 这可能吗,或者有其他方法可以实现吗? thanks 谢谢

   <script> 
       function changeProperty(mine , property , value){
          $(mine).css({ property :  value }); //if I change the property to 'background' for example, it works properly, but I want property to remain so that i will not be changing the property from time to time
       }
   </script>
   <div  onclick="changeProperty(this, 'background', 'red');">
      change background
   </div>
   <div  onclick="changeProperty(this, 'font-size', '15px');">
      change font size
   </div> 
   <div  onclick="changeProperty(this, 'font', 'Arial');">
      change font
   </div>

That's because you pass to css method an object containing property property. 那是因为您将包含property property的对象传递给css方法。 In this case it's better to pass two arguments to css method: 在这种情况下,最好将两个参数传递给css方法:

function changeProperty(mine , property , value){
    $(mine).css(property, value);
}

However, if you want to pass an object, you can dynamically build it: 但是,如果要传递对象,则可以动态构建它:

function changeProperty(mine , property , value){
    var obj = {};
    obj[property] = value;
    $(mine).css(obj);
}

Another improvement is to use this value inside of the function (that is the same with mine in this case): 另一个改进是在函数内部使用了this值(在这种情况下与mine的相同):

function changeProperty(property , value){
    $(this).css(...);
}

Then in HTML you will have changeProperty.call(this, "font", "Arial") . 然后在HTML中,您将具有changeProperty.call(this, "font", "Arial")

.css( propertyName, value )

Set one or more CSS properties for the set of matched elements. 为匹配的元素集设置一个或多个CSS属性。

  • propertyName : A CSS property name. propertyName :CSS属性名称。

    Type: String 类型: String

  • value : A value to set for the property. value :为属性设置的值。

    Type: String or Number 类型:字符串或数字

Try passing the values into the function by wrapping them directly into an object: 尝试通过将值直接包装到对象中来将值传递给函数:

  <script> 
       function changeProperty(mine, css){ 
          $(mine).css(css);
       }
   </script>
   <div  onclick="changeProperty(this, {'background' : 'red'});">
      change background
   </div>
   <div  onclick="changeProperty(this, {'font-size' : '30px'});">
      change font size
   </div> 
   <div  onclick="changeProperty(this, {'font-family' : 'Arial'});">
      change font
   </div>

http://jsfiddle.net/kLd6vp9c/ http://jsfiddle.net/kLd6vp9c/

Noticed in your question, you had some doubt when choosing the jquery framework to implement this feature. 在您的问题中注意到,在选择实现该功能的jquery框架时,您有一些疑问。 I think the time is approaching where we might have to start letting go if jquery and looking into a more native approach, HTML5 is personally seeing to it that it happens. 我认为现在是时候了,如果要使用jquery并开始研究更本地化的方法,那么我们可能必须开始放手了,HTML5亲自看到它发生了。 Here is an open minded and a more bold approach, an alternative if you may, to solving your problem and giving a performance boost to you code. 这是一种开放的思路和更大胆的方法,如果可以的话,可以选择解决问题并提高代码性能的方法。

function changeProperty(mine , property , value){
mine.style[property] = value;
}

Refer to this link to find out the exact property names you are trying to apply, http://www.w3schools.com/jsref/dom_obj_style.asp Thats it!! 请参阅此链接以找到要尝试应用的确切属性名称, http://www.w3schools.com/jsref/dom_obj_style.asp就是这样! call your onclick event handler exactly like it is. 完全按原样调用您的onclick事件处理程序。 This post is not to knock on jquery, i'm a huge fan myself.. God knows i learned a ton from that framework. 这篇文章不是要敲jquery,我自己也是一个忠实的粉丝。。上帝知道我从那个框架中学到了很多。 But its high time we start implementing native code and thinking about leveraging performance from all the hard work HTML5 is putting into promoting the web as a platform. 但是到了时候,我们开始实施本机代码,并思考如何利用HTML5的所有辛苦工作来提高性能,从而促进了Web作为平台的发展。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM