-3

I have this code that makes a box with information follow the mouse. It's really simple, just checks the custom attribute "description" in the div that you hover over and puts that in the box. However, I want to make it so if that div also has a certain CSS class, then it would put other information in the box, in addition to the other code still working.

$(document).ready(function(){
$(".hover").mousemove(function(e){
    if ("div").hasclass("item"){
        alert("div hasclass item");
    } else {
        var description = $(this).attr("description");
        $("#hoverdiv").text(description).show();
        $("#hoverdiv").css("top", e.clientY+10).css("left", e.clientX+5);
    }

}).mouseout(function(){
    $("#hoverdiv").hide();
});

});

that's the code I have now. None of the hovers in my page work at all. This is the code that works. It's identical in every way, except no if statement.

$(document).ready(function(){
$(".hover").mousemove(function(e){
    var description = $(this).attr("description");
    $("#hoverdiv").text(description).show();
    $("#hoverdiv").css("top", e.clientY+10).css("left", e.clientX+5);
}).mouseout(function(){
    $("#hoverdiv").hide();
});

});

I've tried time and time again to get this to work, and through my testing, it would seem that simply adding an if statement breaks the entire thing. I have absolutely no idea how to proceed or how to fix it.

4

2 回答 2

3

罪魁祸首。。

 if ("div")

也许你在尝试

if($("div").something()){
}
于 2012-07-02T16:19:24.347 回答
2
if ("div").hasclass("item") {

应该:

if ( $("div").hasClass("item") ) {

对于更多,您还可以测试:

if ( $("div").is(".item") ) {

阅读有关 jQuery 的信息.is()

于 2012-07-02T16:20:12.330 回答