Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用jQuery从HTML中获取特定值的最佳方法_Javascript_Jquery_Html - Fatal编程技术网

Javascript 使用jQuery从HTML中获取特定值的最佳方法

Javascript 使用jQuery从HTML中获取特定值的最佳方法,javascript,jquery,html,Javascript,Jquery,Html,我有以下一页: <body> ... ... ... <center> <div style="margin-top: -10px; font-size: 8pt; font-weight: bold; color: #909090">2016-04-01</div> </center> <br> <table align="center" width="85%">

我有以下一页:

<body>
...
...
...
  <center>
      <div style="margin-top: -10px; font-size: 8pt; font-weight: bold; color: #909090">2016-04-01</div>
  </center>
  <br>
         <table align="center" width="85%">
            <tbody>
               <tr>
                  <td>
                     <div class="stats-heading">Subscriptions</div>
                     <center>
                        <table cellpadding="0" cellspacing="0" border="0">
                           <tbody>
                              <tr>
                                 <td colspan="9" class="datacell">[ 11 total ]</td>
                              </tr>
                              <tr>
                                 <td class="headingcell">Photo<br>ID</td>
                                 <td class="headingcell">Thumbnail</td>
                                 <td class="headingcell">Downloads</td>
                                 <td class="headingcell">Earned</td>
                                 <td align="center" width="40">
                                    <div style="width: 1px; height: 100%; border-left: 1px solid #E0E0E0"></div>
                                 </td>
                                 <td class="headingcell">Photo<br>ID</td>
                                 <td class="headingcell">Thumbnail</td>
                                 <td class="headingcell">Downloads</td>
                                 <td class="headingcell">Earned</td>
                              </tr>
                              <tr>
                                 <td class="datacellsm" align="center">
                                    <a class="link" href="http://www.website.com/pic.mhtml?id=117501544" target="pic">117501544</a>
                                 </td>
                                 <td align="center">
                                    <div class="thumb_image_container" style="display: block; width: 100px; height: 67px;   margin: 14px 0px ">
                                       <a onclick="if (window.showPhotoDetails) showPhotoDetails(this); return !window.search" x_website_src="" href="http://www.website.com/pic.mhtml?id=117501544">
                                       <img class="thumb_image" onmouseover="if (window.previewThumb) previewThumb(this)" onmouseout="if (window.cancelPreview) cancelPreview(this)" src="http://thumb1.website.com/thumb_small/1207028/117501544/stock-photo-male-white-tipped-plantcutter-phytotoma-rutila-joi-sa-fafa-america-117501544.jpg" alt="Male White-tipped Plantcutter (Phytotoma rutila). joi, sa, fafa America. - stock photo" title="Male White-tipped Plantcutter (Phytotoma rutila). joi, sa, fafa America. - stock photo" align="absmiddle" border="0" itemprop="image">
                                       </a>
                                    </div>
                                 </td>
                                 <td class="datacell" align="center">1</td>
                                 <td class="datacell" align="center">$0.33</td>
                                 <td align="center" width="40">
                                    <div style="width: 1px; height: 100%; border-left: 1px solid #E0E0E0"></div>
                                 </td>
                                 <td class="datacellsm" align="center">
                                    <a class="link" href="http://www.website.com/pic.mhtml?id=117931651" target="pic">117931651</a>
                                 </td>
                                 <td align="center">
                                    <div class="thumb_image_container" style="display: block; width: 100px; height: 67px;   margin: 14px 0px ">
                                       <a onclick="if (window.showPhotoDetails) showPhotoDetails(this); return !window.search" x_website_src="" href="http://www.website.com/pic.mhtml?id=117931651">
                                       <img class="thumb_image" onmouseover="if (window.previewThumb) previewThumb(this)" onmouseout="if (window.cancelPreview) cancelPreview(this)" src="http://thumb7.website.com/thumb_small/1207028/117931651/stock-photo-yellow-billed-pintail-anas-georgica-swimming-with-reflection-on-water-joi-sa-117931651.jpg" alt="Yellow-billed Pintail (Anas georgica) swimming with reflection on water. joi, sa, fafa America. - stock photo" title="Yellow-billed Pintail (Anas georgica) swimming with reflection on water. joi, sa, fafa America. - stock photo" align="absmiddle" border="0" itemprop="image">
                                       </a>
                                    </div>
                                 </td>
                                 <td class="datacell" align="center">1</td>
                                 <td class="datacell" align="center">$0.33</td>
                              </tr>
                              <tr>
                                 <td class="datacellsm" align="center">
                                    <a class="link" href="http://www.website.com/pic.mhtml?id=121414813" target="pic">121414813</a>
                                 </td>
                                 <td align="center">
                                    <div class="thumb_image_container" style="display: block; width: 100px; height: 68px;   margin: 14px 0px ">
                                       <a onclick="if (window.showPhotoDetails) showPhotoDetails(this); return !window.search" x_website_src="" href="http://www.website.com/pic.mhtml?id=121414813">
                                       <img class="thumb_image" onmouseover="if (window.previewThumb) previewThumb(this)" onmouseout="if (window.cancelPreview) cancelPreview(this)" src="http://thumb101.website.com/thumb_small/1207028/121414813/stock-photo-sedge-wren-cistothorus-platensis-joi-sa-fafa-america-121414813.jpg" alt="Sedge Wren (Cistothorus platensis), joi, sa, fafa America. - stock photo" title="Sedge Wren (Cistothorus platensis), joi, sa, fafa America. - stock photo" align="absmiddle" border="0" itemprop="image">
                                       </a>
                                    </div>
                                 </td>
                                 <td class="datacell" align="center">1</td>
                                 <td class="datacell" align="center">$0.33</td>
                                 <td align="center" width="40">
                                    <div style="width: 1px; height: 100%; border-left: 1px solid #E0E0E0"></div>
                                 </td>
                                 <td class="datacellsm" align="center">
                                    <a class="link" href="http://www.website.com/pic.mhtml?id=130744133" target="pic">130744133</a>
                                 </td>
                                 <td align="center">
                                    <div class="thumb_image_container" style="display: block; width: 100px; height: 67px;   margin: 14px 0px ">
                                       <a onclick="if (window.showPhotoDetails) showPhotoDetails(this); return !window.search" x_website_src="" href="http://www.website.com/pic.mhtml?id=130744133">
                                       <img class="thumb_image" onmouseover="if (window.previewThumb) previewThumb(this)" onmouseout="if (window.cancelPreview) cancelPreview(this)" src="http://thumb101.website.com/thumb_small/1207028/130744133/stock-photo-two-roseate-spoonbills-platalea-ajaja-balancing-on-a-dead-branch-joi-sa-fafa-130744133.jpg" alt="Two Roseate Spoonbills (Platalea ajaja) balancing on a dead branch. joi, sa, fafa America. - stock photo" title="Two Roseate Spoonbills (Platalea ajaja) balancing on a dead branch. joi, sa, fafa America. - stock photo" align="absmiddle" border="0" itemprop="image">
                                       </a>
                                    </div>
                                 </td>
                                 <td class="datacell" align="center">1</td>
                                 <td class="datacell" align="center">$0.33</td>
                              </tr>
                              <tr>
                                 <td class="datacellsm" align="center">
                                    <a class="link" href="http://www.website.com/pic.mhtml?id=130744136" target="pic">130744136</a>
                                 </td>
                                 <td align="center">
                                    <div class="thumb_image_container" style="display: block; width: 67px; height: 100px;   margin: 14px 0px ">
                                       <a onclick="if (window.showPhotoDetails) showPhotoDetails(this); return !window.search" x_website_src="" href="http://www.website.com/pic.mhtml?id=130744136">
                                       <img class="thumb_image" onmouseover="if (window.previewThumb) previewThumb(this)" onmouseout="if (window.cancelPreview) cancelPreview(this)" src="http://thumb9.website.com/thumb_small/1207028/130744136/stock-photo-spectacled-tyrant-hymenops-perspicillatus-on-a-dead-branch-joi-sa-fafa-america-130744136.jpg" alt="Spectacled Tyrant (Hymenops perspicillatus) on a dead branch. joi, sa, fafa America. - stock photo" title="Spectacled Tyrant (Hymenops perspicillatus) on a dead branch. joi, sa, fafa America. - stock photo" align="absmiddle" border="0" itemprop="image">
                                       </a>
                                    </div>
                                 </td>
                                 <td class="datacell" align="center">1</td>
                                 <td class="datacell" align="center">$0.33</td>
                                 <td align="center" width="40">
                                    <div style="width: 1px; height: 100%; border-left: 1px solid #E0E0E0"></div>
                                 </td>
                                 <td class="datacellsm" align="center">
                                    <a class="link" href="http://www.website.com/pic.mhtml?id=131579261" target="pic">131579261</a>
                                 </td>
                                 <td align="center">
                                    <div class="thumb_image_container" style="display: block; width: 100px; height: 66px;   margin: 14px 0px ">
                                       <a onclick="if (window.showPhotoDetails) showPhotoDetails(this); return !window.search" x_website_src="" href="http://www.website.com/pic.mhtml?id=131579261">
                                       <img class="thumb_image" onmouseover="if (window.previewThumb) previewThumb(this)" onmouseout="if (window.cancelPreview) cancelPreview(this)" src="http://thumb7.website.com/thumb_small/1207028/131579261/stock-photo-acoustic-guitar-with-very-shallow-depth-of-field-focus-on-strings-black-white-131579261.jpg" alt="Acoustic Guitar with very shallow depth of field, focus on strings. Black &amp; White - stock photo" title="Acoustic Guitar with very shallow depth of field, focus on strings. Black &amp; White - stock photo" align="absmiddle" border="0" itemprop="image">
                                       </a>
                                    </div>
                                 </td>
                                 <td class="datacell" align="center">1</td>
                                 <td class="datacell" align="center">$0.33</td>
                              </tr>
                              <tr>
                                 <td class="datacellsm" align="center">
                                    <a class="link" href="http://www.website.com/pic.mhtml?id=183273761" target="pic">183273761</a>
                                 </td>
                                 <td align="center">
                                    <div class="thumb_image_container" style="display: block; width: 75px; height: 100px;   margin: 14px 0px ">
                                       <a onclick="if (window.showPhotoDetails) showPhotoDetails(this); return !window.search" x_website_src="" href="http://www.website.com/pic.mhtml?id=183273761">
                                       <img class="thumb_image" onmouseover="if (window.previewThumb) previewThumb(this)" onmouseout="if (window.cancelPreview) cancelPreview(this)" src="http://thumb7.website.com/thumb_small/1207028/183273761/stock-photo-illioneus-giant-owl-caligo-illioneus-pampeiro-butterfly-resting-in-a-tree-s-bark-paranaense-183273761.jpg" alt="Illioneus Giant Owl (Caligo illioneus pampeiro) butterfly resting in a tree's bark. Paranaense forest, Misiones, sa, fafa America - stock photo" title="Illioneus Giant Owl (Caligo illioneus pampeiro) butterfly resting in a tree's bark. Paranaense forest, Misiones, sa, fafa America - stock photo" align="absmiddle" border="0" itemprop="image">
                                       </a>
                                    </div>
                                 </td>
                                 <td class="datacell" align="center">1</td>
                                 <td class="datacell" align="center">$0.33</td>
                                 <td align="center" width="40">
                                    <div style="width: 1px; height: 100%; border-left: 1px solid #E0E0E0"></div>
                                 </td>
                                 <td class="datacellsm" align="center">
                                    <a class="link" href="http://www.website.com/pic.mhtml?id=232608718" target="pic">232608718</a>
                                 </td>
                                 <td align="center">
                                    <div class="thumb_image_container" style="display: block; width: 67px; height: 100px;   margin: 14px 0px ">
                                       <a onclick="if (window.showPhotoDetails) showPhotoDetails(this); return !window.search" x_website_src="" href="http://www.website.com/pic.mhtml?id=232608718">
                                       <img class="thumb_image" onmouseover="if (window.previewThumb) previewThumb(this)" onmouseout="if (window.cancelPreview) cancelPreview(this)" src="http://thumb1.website.com/thumb_small/1207028/232608718/stock-photo-many-coloured-rush-tyrant-tachuris-rubrigastra-perched-on-reed-over-water-joi-sa-232608718.jpg" alt="Many-coloured Rush Tyrant (Tachuris rubrigastra) perched on reed over water. joi, sa, fafa America. - stock photo" title="Many-coloured Rush Tyrant (Tachuris rubrigastra) perched on reed over water. joi, sa, fafa America. - stock photo" align="absmiddle" border="0" itemprop="image">
                                       </a>
                                    </div>
                                 </td>
                                 <td class="datacell" align="center">1</td>
                                 <td class="datacell" align="center">$0.33</td>
                              </tr>
                              <tr>
                                 <td class="datacellsm" align="center">
                                    <a class="link" href="http://www.website.com/pic.mhtml?id=238411516" target="pic">238411516</a>
                                 </td>
                                 <td align="center">
                                    <div class="thumb_image_container" style="display: block; width: 100px; height: 66px;   margin: 14px 0px ">
                                       <a onclick="if (window.showPhotoDetails) showPhotoDetails(this); return !window.search" x_website_src="" href="http://www.website.com/pic.mhtml?id=238411516">
                                       <img class="thumb_image" onmouseover="if (window.previewThumb) previewThumb(this)" onmouseout="if (window.cancelPreview) cancelPreview(this)" src="http://thumb9.website.com/thumb_small/1207028/238411516/stock-photo-raw-organic-amaranth-grains-in-a-wooden-spoon-238411516.jpg" alt="Raw organic Amaranth grains in a wooden spoon. - stock photo" title="Raw organic Amaranth grains in a wooden spoon. - stock photo" align="absmiddle" border="0" itemprop="image">
                                       </a>
                                    </div>
                                 </td>
                                 <td class="datacell" align="center">1</td>
                                 <td class="datacell" align="center">$0.33</td>
                                 <td align="center" width="40">
                                    <div style="width: 1px; height: 100%; border-left: 1px solid #E0E0E0"></div>
                                 </td>
                                 <td class="datacellsm" align="center">
                                    <a class="link" href="http://www.website.com/pic.mhtml?id=238411519" target="pic">238411519</a>
                                 </td>
                                 <td align="center">
                                    <div class="thumb_image_container" style="display: block; width: 100px; height: 66px;   margin: 14px 0px ">
                                       <a onclick="if (window.showPhotoDetails) showPhotoDetails(this); return !window.search" x_website_src="" href="http://www.website.com/pic.mhtml?id=238411519">
                                       <img class="thumb_image" onmouseover="if (window.previewThumb) previewThumb(this)" onmouseout="if (window.cancelPreview) cancelPreview(this)" src="http://thumb7.website.com/thumb_small/1207028/238411519/stock-photo-raw-organic-amaranth-grains-texture-background-238411519.jpg" alt="Raw organic Amaranth grains. Texture. Background. - stock photo" title="Raw organic Amaranth grains. Texture. Background. - stock photo" align="absmiddle" border="0" itemprop="image">
                                       </a>
                                    </div>
                                 </td>
                                 <td class="datacell" align="center">1</td>
                                 <td class="datacell" align="center">$0.33</td>
                              </tr>
                              <tr>
                                 <td class="datacellsm" align="center">
                                    <a class="link" href="http://www.website.com/pic.mhtml?id=238987900" target="pic">238987900</a>
                                 </td>
                                 <td align="center">
                                    <div class="thumb_image_container" style="display: block; width: 77px; height: 100px;   margin: 14px 0px ">
                                       <a onclick="if (window.showPhotoDetails) showPhotoDetails(this); return !window.search" x_website_src="" href="http://www.website.com/pic.mhtml?id=238987900">
                                       <img class="thumb_image" onmouseover="if (window.previewThumb) previewThumb(this)" onmouseout="if (window.cancelPreview) cancelPreview(this)" src="http://thumb1.website.com/thumb_small/1207028/238987900/stock-photo-sephanoides-sephaniodes-feeding-on-a-quintral-s-tristerix-tetrandrus-nectar-joi-sa-238987900.jpg" alt="Sephanoides sephaniodes feeding on a Quintral's (Tristerix tetrandrus) nectar. joi, sa, fafa America. - stock photo" title="Sephanoides sephaniodes feeding on a Quintral's (Tristerix tetrandrus) nectar. joi, sa, fafa America. - stock photo" align="absmiddle" border="0" itemprop="image">
                                       </a>
                                    </div>
                                 </td>
                                 <td class="datacell" align="center">1</td>
                                 <td class="datacell" align="center">$0.33</td>
                                 <td align="center" width="40">
                                    <div style="width: 1px; height: 100%; border-left: 1px solid #E0E0E0"></div>
                                 </td>
                              </tr>
                           </tbody>
                        </table>
                     </center>
                     <br><br>
                     <div class="stats-heading">On Demand Downloads</div>
                     <br>
                     <center>
                        <div style="font-weight: bold; color: #585858; font-size: 7pt; margin: 25px 0px">None of your pictures were downloaded from an On Demand account on this date.</div>
                     </center>
                     <div class="stats-heading">Enhanced Downloads</div>
                     <br>
                     <center>
                        <div style="font-weight: bold; color: #585858; font-size: 7pt; margin: 25px 0px">None of your pictures were downloaded via an Enhanced subscription on this date.</div>
                     </center>
                     <div class="stats-heading"></div>
                     <div class="stats-heading">Referred Downloads</div>
                     <style type="text/css">
                        .stats-figure {
                        padding: 2px; 
                        font-size: 11px; 
                        color: #70a040;
                        }
                     </style>
                     <div class="stats-message">
                        None of your <a href="/refinfo.mhtml">referred photographers</a> had downloads on this date.    
                     </div>
                     <div class="stats-heading">Referred Footage</div>
                     <style type="text/css">
                        .stats-figure {
                        padding: 2px; 
                        font-size: 11px; 
                        color: #70a040;
                        }
                     </style>
                     <div class="stats-message">
                        None of your <a href="/reffootage.mhtml">referred videographers</a> had downloads on this date. 
                     </div>
                     <div class="stats-heading">Referred Orders</div>
                     <style type="text/css">
                        .stats-figure {
                        padding: 2px; 
                        font-size: 11px; 
                        color: #70a040;
                        }
                     </style>
                     <div class="stats-message">
                        You didn't <a href="/reforder.mhtml">refer any photo buyers</a> on this date.   
                     </div>
                     <div class="stats-heading">Footage Cart Sales</div>
                     <table align="center" width="85%">
                        <tbody>
                           <tr>
                              <td>
                                 <center>
                                    <div class="stats-message">None of your footage clips were downloaded via shopping cart on this date.</div>
                                 </center>
                              </td>
                           </tr>
                        </tbody>
                     </table>
                     <div class="stats-heading">Clip Packs</div>
                     <table align="center" width="85%">
                        <tbody>
                           <tr>
                              <td>
                                 <center>
                                    <div class="stats-message">None of your footage clips were downloaded via subscription on this date.</div>
                                 </center>
                              </td>
                           </tr>
                        </tbody>
                     </table>
                     <div class="stats-heading">Single &amp; Other Downloads</div>
                     <br>
                     <center>
                        <div style="font-weight: bold; color: #585858; font-size: 7pt; margin: 25px 0px">No images downloaded yet</div>
                     </center>
                     <div class="stats-heading">Delayed Earnings</div>
                     <div class="stats-message">You have 0 <a href="delayed_earnings.mhtml">delayed earning(s)</a> on this day, for $0.</div>
                  </td>
               </tr>
            </tbody>
         </table>
         <p>
         </p>
      </div>
...
...
</body>
从每个
元素中,我需要获得第一个
TD
的值,该值包含ID。在上一个示例中:117501544


还有一些(不是全部)其他
TD
s的值,例如包含价格$0.33的值。

我可能会这样做:

函数getPhotoEarnings(){ var收益=[]; var relevantTable=$(“a[class='link'])。最近的(“表格”); var trs=相关表。查找(“tr”);
对于(var i=2;i来说,很遗憾您无法控制生成的html,因为您所包含的代码片段似乎有点混乱

下面是我在jsfiddle()上与您的html代码片段结合的粗略版本。我确信这不是最好/最优雅的方式

我已经包含了下面的代码来解释:

$('table table > tbody > tr').each(function() {
var $id = $(this).find('td a[target="pic"]:first');
if($id.length > 0)
{
  alert($id.text());
  alert($id.parent().siblings().next().next().html());
}
});
第一行上的jquery选择器跳过第一个表,因为它似乎只用于布局,抓取第二个表的tbody及其所有行,因为根据您的问题,这是您的项所在的位置。选择器的结果立即循环

$('table table > tbody > tr').each(function() {
在循环中(对于每一行),我们找到第一列,它有一个带有pic目标属性的锚点。我们只按照您指定的方式抓取第一列,因为该行并排显示两个项目

var $id = $(this).find('td a[target="pic"]:first');
搜索可能不成功,因为前两个表行用作表头,所以我们检查是否成功

if($id.length > 0)
成功后,我们显示锚定标记文本,然后通过遍历找到的id锚定的父表列,然后告诉jquery获取该列的所有同级并从中转到第二个同级以获取价格,从而获取价格

alert($id.text());
alert($id.parent().siblings().next().next().html());

你可以控制html的生成吗?不,我不能控制生成过程…所以这不仅仅是一行。你需要一种方法来为每一行执行此操作?是的,没错。行数未知。可能从0到500。
if($id.length > 0)
alert($id.text());
alert($id.parent().siblings().next().next().html());