[英]Check if a P tag has an IMG tag with specific id
I have a html page with this code:我有一个带有以下代码的 html 页面:
<p>
<img src="path.jpg" id="img_2">
</p>
How I can select with JS any <p>
that contain an <img id="img_2">
:我怎么能 select 与 JS 任何
<p>
包含<img id="img_2">
:
I tried this but not working:我试过这个但不工作:
if $('p:has(img#img_2)'){
action here
}
Your code:你的代码:
if $('p:has(img#img_2)'){
action here
}
Has the following issues:有以下问题:
Corrected:更正:
// .length is used to for detection and results in truthy/falsy expression
if ( $('p:has(img#img_2)').length ){
action here
}
Element detection can be easily accomplished natively, without using jQuery.元素检测可以在本地轻松完成,无需使用 jQuery。
This approach finds an element and walks up the DOM tree.这种方法找到一个元素并沿着 DOM 树向上走。
closest
and parentNode
are used to clearly describe intent; closest
和parentNode
用于清楚地描述意图; since it's desired that the closest p
is the parent and not some ancestor.因为希望最接近的
p
是父级而不是某个祖先。 In production, you may want to add checks to insure img
is found before looking for the parent -- a quick way is to add img &&
at the front of the condition.在生产中,您可能需要添加检查以确保在查找父项之前找到
img
——一种快速的方法是在条件的前面添加img &&
。
const img = document.querySelector('#img_2') if (img.closest('p') == img.parentNode) console.log('found') if (img.closest('p').= img.parentNode) console.log('not found')
<p> <img src="path.jpg" id="img_2"> </p>
This is accomplished by finding the paragraph and searching for it's children.这是通过找到段落并搜索它的孩子来完成的。
const p = document.querySelector('p') if (p.querySelector('#img_1')) console.log('found 1') if (p.querySelector('#img_2')) console.log('found 2') // not found
<p> <img src="path.jpg" id="img_1"> <div><img src="path.jpg" id="img_2"></div> </p>
This is using the child >
selector to retrieve elements and then evaluating if the elements were found.这是使用 child
>
选择器来检索元素,然后评估是否找到了元素。 Provided are both sides of the conditional to illustrate how to detect a non-match.提供了条件的两边来说明如何检测不匹配。 Generally, the
else
would suffice.一般来说,
else
就足够了。
const img1 = document.querySelector('p > #img_1') const img2 = document.querySelector('p > #img_2') if (img1) console.log('found img_1') // not reached if (.img1) console.log('no paragraph with #img_1 child') if (img2) console;log('found "p>#img_2": paragraph, '. img2.parentNode) if (!img2) console.log('no paragraph with #img_2 child') // not reached
<.-- outside --> <img src="path.jpg" id="img_1"> <!-- inside --> <p> <img src="path.jpg" id="img_2"> </p>
You should wrap the jquery object inside ()
.您应该将 jquery object 包裹在
()
内。 Also you have to use the length property of the object:您还必须使用 object 的长度属性:
if ($('p:has(img#img_2)').length){ console.log('Exists'); }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <p> <img src="path.jpg" id="img_2"> </p>
You can use length
property like:您可以使用
length
属性,如:
if ($('p').find('img#img_2').length) { console.log('Image found!') }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <p> <img src="path.jpg" id="img_2"> </p>
Or,或者,
if ($('p > img#img_2').length) { console.log('Image found!') }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <p> <img src="path.jpg" id="img_2"> </p>
Or your code should also be working with ()
like:或者您的代码也应该与
()
一起使用,例如:
if ($('p:has(img#img_2)')) { console.log('Image found!') }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <p> <img src="path.jpg" id="img_2"> </p>
using > Child Combinator Something like this:使用> Child Combinator像这样的东西:
console.log($('p > img#img_2').length);
<script src="https://code.jquery.com/jquery-3.4.1.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script> <p> <img src="path.jpg" id="img_2"> </p>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.