jquery find - 如果类元素“.myclass”是位于主体之内,但位于其他元素之外

发布于 2024-11-16 09:28:44 字数 548 浏览 4 评论 0原文

由于我有多个正确答案,我正在回答

感谢您的帮助,

这是这里的解决方案

$('body').children('.class_to_find');

感谢

抱歉,示例格式错误,这个降价系统不好。

我的文档中可能有超过 1 个具有相同类的元素,但我需要找到除 body 之外的任何元素之外的元素,例如

<body>
    <div class="class_to_find">my div</div>
    <div class="testing">
       <div class="class_to_find">my other div</div>
    </div>
</body>

我只想捕获“我的 div”,但当然这

$('body').find('.class_to_find');

确实捕获了所有元素,因为它们都在身体里了,我该怎么办?

Since I've got more than one correct answer, I'm answering

Thanks for your help

this was the sollution here

$('body').children('.class_to_find');

Thanks

Sorry for bad formating of example, this markdown system in not good.

I may have more than 1 element with the same class in my document, but I need to just find those who are outside any element but body, example

<body>
    <div class="class_to_find">my div</div>
    <div class="testing">
       <div class="class_to_find">my other div</div>
    </div>
</body>

I just want to catch 'my div', but of course this

$('body').find('.class_to_find');

does catch all, since they're all within body, what should I do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

吹梦到西洲 2024-11-23 09:28:44

使用直接父子选择器:

$('body > .class_to_find')....

.children(),它仅搜索直接后代:

$('body').children('.class_to_find')...

Use the direct parent-child selector:

$('body > .class_to_find')....

or .children(), which only searches direct descendents:

$('body').children('.class_to_find')...
゛时过境迁 2024-11-23 09:28:44

您需要使用 children 方法,因为它仅在直接子级中搜索。

尝试:

$('body').children('.class_to_find');

You need to use children method as it searches only within immediate children.

Try:

$('body').children('.class_to_find');
遇到 2024-11-23 09:28:44

从你的问题来看,这听起来像是可行的:

$('body').children('.class_to_find'); 

From your question, it sounds like this would work :

$('body').children('.class_to_find'); 
ぃ弥猫深巷。 2024-11-23 09:28:44

试试这个

$('body>div.class_to_find');

Try this

$('body>div.class_to_find');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文