English 中文(简体)
同一类不同女婴的轮椅
原标题:jquery change css for different ids with the same class

i 同一类别有3个小区。

<div id="qq1" class="cl">sentence</div>
<div id="qq2" class="cl">sentence</div>
<div id="qq3" class="cl">sentence</div>

i 她想知道,如何为3 divs(call类)使用一种功能的 mo而写正确的代码,并退还用于改动的正确id。

<script>
$( .cl ).live( mouseenter , function() {
    var currentId = $(this).attr( id );
    $( currentId ).css("background-color", "#99cc33");
    $( currentId ).css("color", "white");
});
$( .cl ).live( mouseleave , function() {
    var currentId = $(this).attr( id );
    $( currentId ).css("background-color", "white");
    $( currentId ).css("color", "#404040");
});
</script>

但是,有一些错误的原因,它不工作。

最佳回答

You don t ever use the currentId variable, instead you try to use a string that contains the letters c, u, r, etc., currentId .

为了使之与这一变量合作,你需要说:$(# + presentId.cs(),例如, CurrentId qq1 ,那么实际上你将说$(#qq1 .cs()。

但是,你根本不需要补贴,因为你可以简单地使用<代码>$(本):

$( .cl ).live( mouseenter , function() {
    $(this).css({"background-color" : "#99cc33",
                 "color" : "white"});
});

$( .cl ).live( mouseleave , function() {
    $(this).css("background-color", "white");
    $(this).css("color", "#404040");
});

在一项 j脏活动手稿中,this系指OM要素本身,而$(this)则为这一要素设定了一条 j子,以便你能够就此提出 j。 注:.css() 接受多个特性的地图,以便你能够用单一电话绘制这些地图——我已经表明,在<条码>中,星号为<<<>mouseleave<>/code>上,但不能使用<条码>,以便你进行比较。

(说明:与你的问题无关,但如果你使用jQuery 1.7+,你应停止使用live()。 由于对.on(-在上有关于如何转换你的代码的指示。 如果你使用“带”;1.7,但“有”; 1.4.2,你应使用

问题回答
<script>
$( .cl ).live( mouseenter , function() {
    var currentId = $(this).attr( id );
    $( #  + currentId).css("background-color", "#99cc33");
    $( #  + currentId).css("color", "white");
});
$( .cl ).live( mouseleave , function() {
    var currentId = $(this).attr( id );
    $( #  + currentId).css("background-color", "white");
    $( #  + currentId).css("color", "#404040");
});
</script>

你们试图把变数称作字面图。 之后再由谁来使用这块 #子,然后把其变数推到这种扼杀。

只是这样做:

<script> 
$( .cl ).live( mouseenter , function() { 
    $(this).css("background-color", "#99cc33"); 
    $(this).css("color", "white"); 
}); 
$( .cl ).live( mouseleave , function() { 
    var currentId = $(this).attr( id ); 
    $(this).css("background-color", "white"); 
    $(this).css("color", "#404040"); 
}); 
</script>
$(document).ready(function() {
  $( .cl ).click(function() {
    var currentId = $(this).attr( id );

    $("#"+currentId).css();
  });
});

这是基本思想,其余只是知识。





相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

jquery ui dialog opens only once

I have a button that opens a dialog when clicked. The dialog displays a div that was hidden After I close the dialog by clicking the X icon, the dialog can t be opened again.

Drop down background url in Safari Issue

selectBox.selectCSS { background: url(/Images/replacementSelectBackground.png) top left no-repeat height:auto; } I have an issue in Safari only where the image is not rendering on top ...

CSS specific for Safari

How do you target specifically safari in css with styles?

Aligning textarea in a form

Ive used the following css code to align my form elements: form { position:relative; } form input { position:absolute; left:11em; } However, the textarea element is not aligned correctly with the ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

CSS problem with page footer

I have defined my page footer in the css file as: #footer { position: absolute; height: 50px; text-align: center; background: #66CCCC; bottom: 0px; left: 0px; width: 100%; height: ...

热门标签