开发者

How to select an image outside of div if there is an image with jquery

开发者 https://www.devze.com 2023-01-15 21:05 出处:网络
The following HTML is output from a CMS. (Yes, it is from the last century) There could be an 开发者_运维技巧image or no image outside of #product_cont. The following code has an image.

The following HTML is output from a CMS. (Yes, it is from the last century)

There could be an 开发者_运维技巧image or no image outside of #product_cont. The following code has an image.

And if there is, there will be only one image outside #product_cont.

Now I'd like to select the image outside of #product_cont if it exist.

...
...
<div id="system">       
    <FORM ACTION="command.asp" METHOD="get" NAME="artForm">
    <TABLE BORDER="0" CELLSPACING="0" CELLPADDING="0">
     <TR>
      <TD VALIGN="top">
        <H1>Lian Li PC-B25FB, Miditower, Sort</H1>
<div id="product_cont">
<p><img border="0" alt="" src="http://www.website.com/10157_00.jpg" /> 
<img border="0" alt="" src="http://www.website.com/10157_01.jpg" /> 
<img border="0" alt="" src="http://www.website.com/10157_02.jpg" /> 
<img border="0" alt="" src="http://www.website.com/10157_03.jpg" />
<!-- Maybe more images here --></p>

Testing testing
</div><!-- End of #system -->
      </TD>
      <TD>
    &nbsp;&nbsp;&nbsp;&nbsp;
      </TD>

<TD VALIGN="top" CLASS="td-main">

<P ALIGN="center">
<IMG SRC="images/articles/10031%5F1%2Ejpg" BORDER="0" ALT="Lian Li PC-B25FB, Miditower, Sort">
<!-- in some pages there won't be any images here -->
</P>

<BR>
<TD VALIGN="top" CLASS="td-main">
...
...
<TD VALIGN="top" CLASS="td-main">
...

I tried $("#system img:last"), it will select the last image in #system when there is no image outside #system.

I assume I need to use if statement as well.

Could anyone tell me how to do it with jquery please?


You could use

$('#system').siblings('img');

which would select images at the same level as #system.

Is this what you need?


I am assuming you're looking for this element:

<IMG SRC="images/articles/10031%5F1%2Ejpg" BORDER="0" ALT="Lian Li PC-B25FB, Miditower, Sort">

You could check for its existence with

$('.td-main img').length > 0;


If the picture you are looking for, always is referenced from "images/articles/", then the following selector could possibly do it

$("#product_cont").closest("tr").find("img[src^='images\\/articles\\/'")
0

精彩评论

暂无评论...
验证码 换一张
取 消