如何改变CSS与jquery? [英] How to change CSS with jquery?

查看:81
本文介绍了如何改变CSS与jquery?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有PHP动态生成的以下HTML。
当我点击活动时,它改为非活动,反之亦然。

I have the following HTML generated dynamically by PHP. When I click active, it changed to inactive and vise versa.

我添加了div class ='status'用于jQuery操作。 (我需要它吗?我可以没有这个div吗?)

I added div class='status' for jquery manipulation. (Do I need it? Can I do it without this div?)

我想改变CSS class ='status'到class ='inactive'当我点击不活动时,改变CSS类为active,以便我可以更改颜色或添加bg图像等。

I want to change CSS class='status' to class='inactive' when I click active and when I click inactive changing CSS class to 'active' as well, so that I can change a color or add bg image etc.

HTML

...
...    
<tr valign='top'>
  <td align='center'>21</td>
  <td>Kontakt</td>
  <td>/kontakt.html</td>
  <td align='center'>
     <div class="status">
       <a href="http://127.0.0.1/ci/index.php/admin/pages/changePageStatus/21">active</a>
     </div>
  </td>
  <td align='center'>...
  ...
  </td>
</tr>

我使用jquery,我需要一些指导。

I am using jquery and I need some guidance.

任何人都可以告诉我一些jquery代码吗?

Can anyone tell me some jquery code please?

- UPDATE -

--UPDATE--

有很多div与calss ='status',所以我怎么能告诉jquery我想改变我点击的css。

There are many div with calss='status', so how can I tell jquery that I want to change the css which I clicked.

- UPDATE 2 -

--UPDATE 2--

我想检查值(活动/非活动)。并将它的值添加为类。 e.x如果它的值是活动的,那么添加css class ='active',如果它是非活动的,那么class ='inactive'等。
我可以这样做吗?
或者你有其他建议我应该怎么做。

I want to check the value(active/inactive). And add it's value as class. e.x If its value is active, then add css class='active' and if it is inactive then class='inactive' etc. Can I do it? Or you have other suggestions how I should do it.

- UPDATE 3 -

--UPDATE 3--

感谢大家。你们摇滚。我测试了一些代码。但问题是,当我点击活动,它更改bg,但然后刷新页面更改为inactive通过php / mysql。所以这意味着每次我点击值(活动/非活动)更改。所以我认为正如我在更新2中所说的,它可能是个好主意,添加css类取决于值。
欢迎更多的建议,我很感激他们。 (我还没有使用AJAX ...)

Thanks everyone. You guys rock. I tested some of codes. But the problem is that when I click active, it change the bg, but then refreshes the page to change to inactive via php/mysql. So this means each time I click the value(active/inactive) changes. So I think as I stated in update 2, it might be good idea to add css class depends on the value. More suggestions are welcome and I appreciate them. (I have not done it with AJAX yet...)

- UPDATE 4 -

--UPDATE 4 --

ID不是一个好主意,因为表是动态创建的。我可以添加ID,但它会添加更多的代码。所以我认为类更好。

Using ID is not a good idea since the table is created dynamically. I can add IDs but it will add more code. So I thought class is better.

- UPDATE 5 -

--UPDATE 5--

如何添加类取决于使用jquery的值

之后,我现在知道我想做什么。

After this, I now know what I want to do.

推荐答案

jQuery具有函数toggleclass(),removeClass()和addClass这些应该可以帮助你。

jQuery has functions toggleclass(), removeClass() and addClass(). These should help you along.

没有看到整个HTML它有点难以建议一个选择器。如果您为您的div(例如mydiv)分配了一个ID,那么您可以拥有

Without seeing the whole HTML its a bit difficult to suggest a selector. If you assigned an id to your div (e.g. mydiv) then you could have

$("#mydiv").removeClass('status').addClass('inactive');

或者只是为了翻转它们:

Or just to flip them regardless:

$("#mydiv").toggleClass('status').toggleClass('inactive');

这使得链接非常好用

这篇关于如何改变CSS与jquery?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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