简体   繁体   English

用jquery更改DOM元素CSS

[英]Change DOM element CSS with jquery Not

I have two clickable headings. 我有两个可点击的标题。 When you click one, it changes blue. 当您单击一个时,它变为蓝色。 If you click the other one, it changes blue, but should "unclick" the other heading and change it back to black. 如果单击另一个,它会变为蓝色,但应“取消单击”另一个标题并将其改回黑色。

I'm trying to do this using jquery not ... 我正在尝试使用jQuery not ...

$("#pal1, #pal2").click(function(event) {
    $(this).css({"color" : "blue"});
    var pal1 = $(this).attr('id');
    var pal2 = $(this).next().attr('id');   
    $(this).not(pal2).css({"color" : "black"}); //if the selected heading is not pal1, or not pal2, changed the one that is not == $(this) back to black.
});

Or something like that. 或类似的东西。


Edit: I know I can do it this way... but wanted to use jQuery functions to reduce code size. 编辑:我知道我可以这样做...但是想使用jQuery函数来减少代码大小。 Plus this isn't very efficient. 再加上这不是很有效。

$("#pal1, #pal2").click(function(event) {
    $(this).css({"color" : "blue"});        
    if ($(this).attr('id') !== $("#pal1").attr('id')) {
        $("#pal1").css({"color" : "black"});
    }
    else {
        $("#pal2").css({"color" : "black"});
    }
});

Set both to black , then set the clicked one to blue . 将两者都设置为black ,然后将单击的对象设置为blue

$("#pal1, #pal2").click(function(event) {
  $("#pal1, #pal2").css({"color" : "black"});
  $(this).css({"color" : "blue"});
});

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

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