jquery选择器get元素,其类位于div中

jquery选择器get元素,其类位于div中,jquery,Jquery,我正在处理jquery选择器。 我有一部分html(简化)看起来像 <div class="tile"> message <a href="#" class="ui-icon ui-icon-pencil">link1</a> <a href="#" class="ui-icon ui-icon-close">linkb</a> </div> 但这不起作用

我正在处理jquery选择器。 我有一部分html(简化)看起来像

<div class="tile">            
      message
      <a href="#" class="ui-icon ui-icon-pencil">link1</a>
      <a href="#" class="ui-icon ui-icon-close">linkb</a>    
</div>
但这不起作用。
这里有人能帮我吗?

试试:
$(this).查找('.ui图标').show()
有几种方法可以将jQuery select限定到当前子树。这是一个使用
.find()
方法的方法

$(function () {
    $(".tileAgenzia").bind("mouseover", function () {
        $(this).find(".ui-icon").show();
    });
    $(".tileAgenzia").bind("mouseout", function () {
        $(this).find(".ui-icon").hide();
    });
});
我可以将此作为一种更简单的选择:

$(function () {
    $(".tileAgenzia").hover(function () {
        $(this).find(".ui-icon").toggle();
    });
});

字符串中的
this
与变量
this
不同。将其包装在jQuery中并使用
find

$(function () {
    $(".tileAgenzia").bind("mouseover", function () {
        $(this).find(".ui-icon").show();
    });
    $(".tileAgenzia").bind("mouseout", function () {
        $(this).find(".ui-icon").hide();
    });
});

我想你的意思可能是:

$(this).find(".ui-icon")
有几种选择:

最小修复 您已经在选择器中使用了“this”;您要做的是使用
$(this)
为鼠标移动到的实际平铺获取jQuery对象,然后使用
find
查找子元素。您最好使用
mouseenter
mouseleave
而不是
mouseover
mouseout

$(function () {
    $(".tileAgenzia").bind("mouseenter", function () {
        $(this).find(".ui-icon").show();
    });
    $(".tileAgenzia").bind("mouseleave", function () {
        $(this).find(".ui-icon").hide();
    });
});
(您最好使用的原因是
mouseover
mouseout
气泡,因此当鼠标在“tileAgenzia”元素的子元素上移动时,您将看到来自这些子元素的消息。)

使用CSS(如果可以的话) 但值得注意的是,除非您必须支持IE6和IE7(有些人也支持),否则您完全可以使用CSS实现这一点,无需JavaScript:

.tileAgenzia .ui-icon {
    display: none;
}
.tileAgenzia:hover .ui-icon {
    display: inline;
}
当鼠标悬停在类为“tileAgenzia”的元素上的任意位置时,其类为“ui icon”的子元素将可见;当鼠标不在其上悬停时,它们不会

更简洁的jQuery 如果您想坚持使用JavaScript解决方案,可以使用
hover
函数,这是(如果您将两个函数传递给它)连接
mouseenter
mouseleave
的快捷方式:

$(function () {
    $(".tileAgenzia").hover(
        // Called on mouseenter
        function () {
            $(this).find(".ui-icon").show();
        },
        // Called on mouseleave
        function () {
            $(this).find(".ui-icon").hide();
        }
    );
});

我相信你想要改变:

$("this .ui-icon")

啊,“this”不应该在撇号内。如果您加载了jQuery,我会用jQuery的方式:

$(".tile").hover(function(){
  $(this).children(".ui-icon").show();
}, function(){
  $(this).children(".ui-icon").hide();
});

要将选择器限制为父元素,请使用以下任一选项:

$(this).find(".ui-icon")
或:


检查你的课程:平铺≠ 所有的答案对你的问题都有一个非常普遍的调整。我想知道它是否会起作用@GG:谢谢,这只是一个输入错误,因为在SO上发送代码时出现了错误。我很惊讶没有其他人建议使用第二种语法。我一直在使用它…这是因为jQuery在内部以
$(this)的形式运行它。查找
,就我个人而言,语法有点笨拙,因为
this
位于选择器的错误一侧。我喜欢CSS解决方案。即使这里有很多人给了我最常见的解决方案,我也会投票认为你的答案被你给出的明智选择所接受。要获得更简洁的答案,请输入我的
$('.ui icon',this)
,你就赢了!;)@约翰:说得更简洁些,没错,我只是不喜欢这么间接。但是我想那些喜欢香肠的人不应该看它的制作(例如,不应该看jQuery源代码)
$("this .ui-icon")
$(this).find(".ui-icon")
$(".tile").hover(function(){
  $(this).children(".ui-icon").show();
}, function(){
  $(this).children(".ui-icon").hide();
});
$(this).find(".ui-icon")
$('.ui-icon', this)