2

试图从 .class 的第二个类创建一个变量。

var post_id = $('.divclass').hasClass('');

$(document).ready(function(){
    $(post_id).click(function() {
        $(this).fadeIn(1000);
    });
});

我知道这是错误的,但也许这里有人可以帮助理解我正在尝试做的事情。提前致谢。

4

3 回答 3

3

所以你需要做的是选择你正在做的不止一个类的项目:

var post_id = $('.divclass').attr('class');
//Now spilt the string by all of the spaces
post_id.split(" ");
//now refer to the string as an array
//lets get the second one.
post_id[1]

所以对于你的情况

//Added selector in this case a class with '.' this can be changed to be appropriate i.e '#' for an ID
$('.'+post_id[1]).click(function() {
    $(this).fadeIn(1000);
});
于 2013-04-15T08:03:38.413 回答
1

post_id的是一个布尔值。您正在尝试将事件处理程序附加到布尔值,而应该将其附加到 DOM 元素。不要使用 has class,而是检索 class 属性:

var post_id = $('.divclass').attr('class');
post_id = post_id.replace('divclass', '');
于 2013-04-15T08:03:58.937 回答
1

如果您有 2 个如下课程:

<div id="trash" class="a b">
    <p>sample</p>
</div>

然后你可以使用 jQuery 选择器如下:

$(document).ready(function(){
    $('.a.b').click(function() {
        $(this).fadeIn(1000);
    });
});

我希望这对你有帮助。

于 2013-04-15T08:05:26.027 回答