JQuery 同级简写
我正在尝试使用我认为有效的速记符号来检测元素的直接兄弟元素。以下是我的代码:
$menuContainer = $('<div></div>').attr('id', 'menuContainer').attr('class', 'menu-container'); //.hide();
$menuContainerInner = $('<div></div>').attr('class', 'container-inner');
$menuContainer.append($menuContainerInner);
$menuContainer.insertAfter($('div#block-views-Menu-block_1'));
$($menuContainer + ' > div.container-inner').css('background', 'red');
在上面的代码中,我动态创建了 2 个 div 并将它们分配给变量以供以后参考。 我将第二个 div 嵌入到第一个 div 中。
因此,使用 $($menuContainer + ' > div.container-inner').css('background', 'red');
我试图查找并更改内部 div 的背景颜色不使用普通的 div-id 路径。
没有一些符号可以做到这一点还是我弄错了?
I am trying to detect an elements direct sibling using a short hand notation which I thought worked. The following is my code:
$menuContainer = $('<div></div>').attr('id', 'menuContainer').attr('class', 'menu-container'); //.hide();
$menuContainerInner = $('<div></div>').attr('class', 'container-inner');
$menuContainer.append($menuContainerInner);
$menuContainer.insertAfter($('div#block-views-Menu-block_1'));
$($menuContainer + ' > div.container-inner').css('background', 'red');
In the above code I dynamically create 2 div and assign them to variables for latter reference.
I embed the second div in the first.
So, using $($menuContainer + ' > div.container-inner').css('background', 'red');
I am trying to find and change the inner-div's background color without using the normal div-id's path.
Isn't there some notation that does this or am I mistaken??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
$menuContainer
是一个对象,您将隐式调用toString
,这可能不是您想要的。我会使用
children
代替:但是如果你喜欢这个(通过对象单独作为根)也可以工作:
免费的实例
$menuContainer
is an object, which you're going to implicitly calltoString
on, which probably isn't what you want.I'd use
children
instead:But if you like this (passing the object separately as the root) also works:
Gratuitous live example
“$menuContainer”似乎是一个jquery对象,而不是一个字符串,我相信你想要的是:
$menuContainer.children('.container-inner');
"$menuContainer" seems to be a jquery object, not a string, I believe what you want is:
$menuContainer.children('.container-inner');
尝试使用选择器的上下文:
Try using the context of the selector: