我想结合我的选择器,它根据鼠标悬停事件选择一个元素。这是我到目前为止所做的。
parentItem.mouseenter(function ()
{
var childItems = $(this).add(this + "li:first");
childItems.show();
});
据我所知,我不能单独使用'this',也不能将两者一起添加,因为它不是字符串。
我如何实现这个概念?感谢。
答案 0 :(得分:1)
你不能this + "li:first"
,this
是DOMElement,而不是字符串。只是做:
$(this).add("li:first");
虽然,这可能不是你想要的。这将在页面上找到第一个<li>
(并将其添加到包含this
的jQuery对象中。)
如果您希望第一个<li>
成为parentItem
的孩子,请尝试以下操作:
$("li:first", this);
(这与$(this).find("li:first");
)
答案 1 :(得分:1)
如果我理解正确,您试图在触发事件的元素下选择第一个li
?您可以将this
用作上下文:
parentItem.mouseenter(function ()
{
var childItems = $("li:first",this);
childItems.show();
});
答案 2 :(得分:0)
您无法连接this
(在本例中为DOM元素),因为它无法强制转换为字符串。 .add
只是在集合中添加了更多元素。 $(this)
已经匹配当前元素(即一个鼠标悬停),所以看起来这可以满足您的需求:
$(this).add("li:first").show();
答案 3 :(得分:0)
我想结合我选择基于元素的Selector 鼠标悬停事件。这是我到目前为止所做的。
你没有指定你想要实现的目标,无论如何你可以使用next()prev()child()parent()jQuery方法
编辑:Ayways的目标只是简单地选择父母孩子的第一个元素吗?