简体   繁体   English

jQuery css()on。('click'…不起作用

[英]JQuery css() on.('click' … doesn't work

Here is my JQuery code: 这是我的JQuery代码:

$(document).ready(function () {
    $("ul.navbar").on("click", "li", function() {
        $(this).find("h2").css("color: pink");
        alert($(this).text());
        alert($(this).find("h2").text());
    });
});

The click generates the alerts correctly, but the color of the text does not change. 单击可以正确生成警报,但是文本的颜色不会改变。 I've tried .addClass() too and that also does not work. 我也尝试过.addClass() ,这也不起作用。 Any ideas? 有任何想法吗? I've perused a lot of the other similar questions but not found them helpful, I'm a real newb to Javascript and still stuck. 我细读了许多其他类似的问题,但没有发现它们有帮助,我是Java语言的真正新手,但仍然受困。

You are using .css() incorrectly - 您使用的.css()错误-

It should be like this - 应该是这样-

$(this).find("h2").css("color","pink");

API ---> http://api.jquery.com/css/ API ---> http://api.jquery.com/css/

Change this: 更改此:

$(this).find("h2").css("color: pink");

to that: 对此:

$(this).find("h2").css("color", "pink");

JQuery css function takes two parameters, you are using it incorrectly. jQuery css函数有两个参数,您使用不正确。

Correct parameters are as below 正确的参数如下

$(this).find("h2").css("color" ,"pink");

或这个:

$(this).find("h2").css( {color: "pink" } );

jQuery's CSS function takes arguments in two styles: jQuery的CSS函数采用两种样式的参数:

  • As a single name/value pair: 作为单个名称/值对:

     .css('stylename' , 'value') 

    Two parameters in the function call, so separated by a comma. 函数调用中的两个参数,因此用逗号分隔。

    ...in which case, your code would look like this: ...在这种情况下,您的代码如下所示:

     .css('color', 'pink')` 

    Note: Both parameters are strings, so must be in quotes. 注意:这两个参数都是字符串,因此必须用引号引起来。 It doesn't matter whether you use single or double quotes. 使用单引号还是双引号都没关系。

  • As an object containing multiple name/value pairs: 作为包含多个名称/值对的对象:

     .css({'stylename':'value' , 'stylename':'value' , etc}) 

    One parameter, but defined as an object, so enclosed in curly braces; 一个参数,但定义为对象,因此用花括号括起来; each name/value separated from each other by a colon; 每个名称/值之间用冒号隔开; and each pair of name/values separated by a comma. 每对名称/值之间用逗号分隔。

    ...in which case, your code would look like this: ...在这种情况下,您的代码如下所示:

     .css({'color': 'pink'})` 

    As before, it doesn't matter about single or double quotes, but note that since this is an object structure, the quotes around the style names are optional (although not in all cases, so it's probably best to stick to always having them). 和以前一样,单引号或双引号无关紧要,但是请注意,由于这是一个对象结构,因此样式名称周围的引号是可选的(尽管并非在所有情况下都如此,因此最好始终使用它们) 。

The first syntax was originally the only one available; 第一种语法最初是唯一可用的语法。 the latter syntax was added in a later version of jQuery to allow more than one style to be set in a single call. 后一种语法是在更高版本的jQuery中添加的,以允许在一个调用中设置多个样式。

If you're only changing a single style value then it doesn't matter which syntax you use. 如果仅更改单个样式值,则使用哪种语法都没有关系。 If you're changing more than one style at a time, then obviously it's better to use the second syntax rather than making multiple calls using the first one. 如果您一次要更改一个以上的样式,那么显然最好使用第二种语法,而不是使用第一种进行多次调用。

Your code seems to work for me: 您的代码似乎对我有用:

$(document).ready(function () {
    $("ul.navbar").on("click", "li", function() {
        $(this).find("h2").css("color","pink");
        alert($(this).text());
        alert($(this).find("h2").text());
    });
});

jsFiddle jsFiddle

Which jQuery are you using? 您正在使用哪个jQuery?

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

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