如何使用JQuery修改具有已知ID的元素的STYLE属性 [英] How to modify STYLE attribute of element with known ID using JQuery

查看:1063
本文介绍了如何使用JQuery修改具有已知ID的元素的STYLE属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3个按钮 - 链接触发一些JavaScript代码,指示是否选择某个按钮,所选按钮的style属性设置为

I got 3 buttons-links triggering some javascript code, with indication if certain button is selected, selected button got style attribute set to

btn brown selected

"btn brown selected"

,而其他按钮将此属性设置为

while other buttons got this attribute set to

btn brown

一次只能选择一个按钮,每个按钮都有不同的唯一id,我的问题是如何使用jquery访问某个按钮来修改它的样式attr。我需要它的唯一的如何通过id单一按钮访问,并修改其风格属性以获得即时更新在屏幕上

only one button can be selected at one time, each of buttons got different unique id, my question is how using jquery access certain button to modify it's style attr. What i need its only how to access by id single button and modify its style attribute to get immediate update on the screen

感谢提前
MTH

thanks in advance MTH

推荐答案

不确定我是否完全理解问题,但

Not sure I completely understand the question but:

$(":button.brown").click(function() {
  $(":button.brown.selected").removeClass("selected");
  $(this).addClass("selected");
});

看起来像你想要的那样。

seems to be along the lines of what you want.

我一定会建议使用类,而不是直接设置CSS,这是有问题的几个原因(例如,删除样式是不重要的,删除类很容易),但如果你想这样:

I would certainly recommend using classes instead of directly setting CSS, which is problematic for several reasons (eg removing styles is non-trivial, removing classes is easy) but if you do want to go that way:

$("...").css("background", "brown");

但是当你想要扭转这种变化时,你把它设置成什么?

But when you want to reverse that change, what do you set it to?

这篇关于如何使用JQuery修改具有已知ID的元素的STYLE属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆