简体   繁体   English

div的InnerHTML

[英]InnerHTML of a div

If you want to retrieve the innerHTML of a div (seen beneath) without using getElementByID (considering this website has several div's with 'quote' as an id), but by referring to the 'field name' (field="bid"). 如果要不使用getElementByID来检索div的innerHTML(如下所示)(考虑到此网站有多个div,且以“ quote”作为ID),而是引用“字段名称”(field =“ bid”)。 How would you do that? 你会怎么做?

<div nowrap="nowrap" id="quote" class="realtimeDetailLayer" source="lightstreamer" 
style="display:inline" table="LS_ProductDetails_TabID" item="X0000010600NL0010379129"
field="bid">0,28</div>

I want to retrieve the information from an existing website. 我想从现有网站检索信息。 See beneath. 见下面。 Here you can see that several div's have the same ID 在这里您可以看到多个div具有相同的ID

<tbody>
  <tr>
    <td style="text-align:right;width:auto;padding:8px 0px;"><nobr>Ref.</nobr>:</td>
    <td class="ext04" style="white-space:nowrap;width:12%;font-weight:bold;">
    EUR
      <strong><div nowrap=nowrap id="quote" class="realtimeDetailLayer" source="lightstreamer" style="display:inline" table="LS_ProductDetails_TabID" item="X0000010600NL0010379129" field="reference">350,85
      </div></strong>
    </td>
    <td style="text-align:right;width:auto;padding:8px 0px;"><nobr>Bied</nobr>:</td>
    <td class="ext04" style="white-space:nowrap;width:12%;font-weight:bold;">
    EUR 
      <div nowrap=nowrap id="quote" class="realtimeDetailLayer" source="lightstreamer" style="display:inline" table="LS_ProductDetails_TabID" item="X0000010600NL0010379129" field="bid">0,28</div>
    </td>
    <td style="text-align:right;width:auto;padding:8px 0px;">
      <nobr>Laat</nobr>:
    </td>
    <td class="ext04" style="white-space:nowrap;width:12%;font-weight:bold;">
    EUR
      <div nowrap=nowrap id="quote" class="realtimeDetailLayer" source="lightstreamer" style="display:inline" table="LS_ProductDetails_TabID" item="X0000010600NL0010379129" field="ask">0,29
      </div>
    </td>
    <td style="text-align:right;width:auto;padding:8px 0px;"><nobr>%</nobr>:</td>
    <td class="ext04" style="white-space:nowrap;width:12%;font-weight:bold;">
      <div nowrap=nowrap id="quote" class="realtimeDetailLayer" source="lightstreamerGeneratedValues" style="display:inline" table="LS_ProductDetails_TabID" item="X0000010600NL0010379129" field="midchangepercent"><span class="extNegative"><nobr>-50,88 %</nobr></span>
      </div>
    </td>
  </tr>
</tbody>

您可以通过简单地解析通过获取父元素和子元素的DOM来实现。

Try the following: 请尝试以下操作:

els = document.getElementsByTagName('div');
for (i=0; i<els.length; i++) {
  if (els[i].field == 'bid') {
    //do whatever with the bid element here.
  }
}

This gets all the divs and checks the field attribute. 这将获取所有divs并检查field属性。

This solution is for the worst case scenario where you don't have any way to have unique ids for the elements 此解决方案适用于最坏的情况,即您无法为元素提供唯一的ID

var divs = document.getElementsByTagName('div');
function getQuoteHtml(key){
    var i;
    for(i =0 ; i < divs.length; i++){
        if(divs[i].getAttribute('field') == key){
            return divs[i].innerHTML;
        }
    }
}

Demo: Fiddle 演示: 小提琴

您可以使用CSS选择器来执行此操作

document.querySelectorAll('[field="bid"]');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM