简体   繁体   English

使用this.id为一个ID更改CSS

[英]Change CSS for one ID using this.id

I have to div like this: 我必须像这样div

<div class="vignettes" id="vignette1"></div>
<div class="vignettes" id="vignette2"></div>

I want to do the same thing on Hover event for both div , let's say change the background to black: 我想在两个div Hover事件上做同样的事情,假设将background更改为黑色:

$('.vignettes').hover(function () 
{
    //$('.vignettes').css("background", "#000");
    $(this.id).css("background", "#000");
}, 
function() 
{  
});

The commented line works, but obviously change both div to black when I hover one of them. 带注释的行有效,但是当我将鼠标悬停在其中时,显然div都更改为黑色。 I want to change only the hovered one. 我只想更改徘徊的那个。 Instead of cloning my hover function with good id s, I want to get the id of the hovered one and change its background dynamically. 我不想使用良好的id克隆我的悬停函数,而是要获取悬停的悬停函数的id并动态更改其background

The code alert(this.id) pops up the good id, so this.id works. 代码alert(this.id)弹出良好的ID,因此this.id可以工作。

But $(this.id).css("background", "#000#"); 但是$(this.id).css("background", "#000#"); doesn't do anything. 什么也没做

JSFiddle sample JSFiddle示例

As this is the target, just use $(this).css("background", "#000"); 因为this 目标,所以只需使用$(this).css("background", "#000");

eg 例如

$('.vignettes').hover(function () 
{
    $(this).css("background", "#000");
}, 
function() 
{  
});

It is a bit silly to use a selector to find the current element by its own id as suggested 如建议的那样,使用选择器通过自己的ID查找当前元素有点愚蠢

In fact, most of the time, you do not even need IDs to do this sort of operation, just use classes to change the styling: 实际上,在大多数情况下,您甚至不需要ID即可执行此类操作,只需使用类来更改样式即可:

eg http://jsfiddle.net/TrueBlueAussie/cLws40vr/8/ 例如http://jsfiddle.net/TrueBlueAussie/cLws40vr/8/

$('.vignettes').hover(function () {
    $(this).addClass("selected");
    console.log("test");
},

function () {
    $(this).removeClass("selected");
});

You just need to use $(this) 您只需要使用$(this)

When you are in a function like this, using $(this) will apply to the current element the event applies to. 当您处于这样的函数中时,使用$(this)将适用于事件所适用的当前元素。

$('.vignettes').hover(function () 
{
   $(this).css("background", "#000");
}, 
function() 
{  

});

Updated JSFiddle - http://jsfiddle.net/cLws40vr/4/ 更新了JSFiddle- http://jsfiddle.net/cLws40vr/4/

As most of the other answers have suggested using this as the selector is the correct way to accomplish what you are trying to do in this case. 正如大多数其他答案所建议的那样,使用此方法作为选择器是完成这种情况下要执行的操作的正确方法。

 $(this).css("background", "#000"); 

There is another error in your code that was causing your original code not to work. 您的代码中还有另一个错误,导致您的原始代码无法正常工作。 To select using an id you need to add the hash symbol to the beginning of the selector string. 要使用ID进行选择,您需要将哈希符号添加到选择器字符串的开头。

 $('#' + this.id).css("background", "#000"); 

I just thought I would point that out in case anyone was wondering why the original code didn't work. 我只是想指出一点,以防万一有人想知道为什么原始代码不起作用。

$(this).css("background", "#000");
alert($(this).attr('id'));

Fiddle http://jsfiddle.net/cLws40vr/5/ 小提琴http://jsfiddle.net/cLws40vr/5/

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

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