是否可以在不使用id字段的情况下获取HTML标记的唯一ID?

时间:2018-04-20 12:45:09

标签: javascript html dom

我试图了解是否有机会从HTML标记/节点获取唯一ID而不使用标记上的id属性,我的情况是这样的:

customlocation.gpx

使用myScript.js我想以独特的方式识别两个div,例如:

xcode

编辑:

有关案件的更多信息:

  1. 我无法在div中添加任何类型的属性。
  2. 我不能使用jQuery
  3. 我不会是创建HTML结构的人
  4. n°的div是随机的,甚至是style属性中的位置和尺寸,所以我也可以<!-- div #1 --> <div style="width:200px; height:200px;"><script src="myScript.js"></script></div> <!-- div #2--> <div style="width:200px; height:200px;"><script src="myScript.js"></script></div>
  5. 提前致谢

2 个答案:

答案 0 :(得分:0)

如果您知道它们出现的顺序,那么在"PricedItineraries": { "AirItinerary": { "OriginDestinationOptions": { "OriginDestinationOption": [{ "FlightSegment": [{ "DepartureAirport": { "LocationCode": "JFK" }, "ArrivalAirport": { "LocationCode": "LAX" }, "MarketingAirline": { "Code": "AA" }, "ArrivalTimeZone": { "GMTOffset": -7 }, "TPA_Extensions": { "eTicket": { "Ind": true } }, "StopQuantity": 0, "ElapsedTime": 376, "ResBookDesigCode": "B", "MarriageGrp": "O", "Equipment": { "AirEquipType": "32B" }, "DepartureDateTime": "2018-07-07T07:00:00", "ArrivalDateTime": "2018-07-07T10:16:00", "FlightNumber": 33, "OnTimePerformance": { "Level": 9 }, "OperatingAirline": { "FlightNumber": 33, "Code": "AA" }, "DepartureTimeZone": { "GMTOffset": -4 } }], "ElapsedTime": 376 }, { "FlightSegment": [{ "DepartureAirport": { "LocationCode": "LAX" }, "ArrivalAirport": { "LocationCode": "JFK" }, "MarketingAirline": { "Code": "AA" }, "ArrivalTimeZone": { "GMTOffset": -4 }, "TPA_Extensions": { "eTicket": { "Ind": true } }, "StopQuantity": 0, "ElapsedTime": 335, "ResBookDesigCode": "B", "MarriageGrp": "O", "Equipment": { "AirEquipType": "32B" }, "DepartureDateTime": "2018-07-09T15:30:00", "ArrivalDateTime": "2018-07-10T00:05:00", "FlightNumber": 2201, "OnTimePerformance": { "Level": 8 }, "OperatingAirline": { "FlightNumber": 2201, "Code": "AA" }, "DepartureTimeZone": { "GMTOffset": -7 } }], "ElapsedTime": 335 }] }, "DirectionInd": "Return" }, "TPA_Extensions": { "ValidatingCarrier": { "Code": "AA" } }, "SequenceNumber": 3, "AirItineraryPricingInfo": { "PTC_FareBreakdowns": { "PTC_FareBreakdown": { "FareBasisCodes": { "FareBasisCode": [{ "BookingCode": "B", "DepartureAirportCode": "JFK", "AvailabilityBreak": true, "ArrivalAirportCode": "LAX", "content": "QUAJZNB3" }, { "BookingCode": "B", "DepartureAirportCode": "LAX", "AvailabilityBreak": true, "ArrivalAirportCode": "JFK", "content": "QUAJZNB3" }] }, "PassengerTypeQuantity": { "Quantity": 1, "Code": "ADT" }, "PassengerFare": { "FareConstruction": { "CurrencyCode": "USD", "DecimalPlaces": 2, "Amount": "249.30" }, "TotalFare": { "CurrencyCode": "USD", "Amount": "296.40" }, "Taxes": { "TotalTax": { "CurrencyCode": "USD", "DecimalPlaces": 2, "Amount": "47.10" }, "Tax": [{ "CurrencyCode": "USD", "DecimalPlaces": 1, "TaxCode": "AY", "Amount": 11.2 }, { "CurrencyCode": "USD", "DecimalPlaces": 1, "TaxCode": "US1", "Amount": 18.7 }, { "CurrencyCode": "USD", "DecimalPlaces": 0, "TaxCode": "XF", "Amount": 9 }, { "CurrencyCode": "USD", "DecimalPlaces": 1, "TaxCode": "ZP", "Amount": 8.2 }] }, "BaseFare": { "CurrencyCode": "USD", "Amount": "249.30" }, "EquivFare": { "CurrencyCode": "USD", "DecimalPlaces": 2, "Amount": "249.30" } }, "Endorsements": { "NonRefundableIndicator": true } } }, "FareInfos": { "FareInfo": [{ "TPA_Extensions": { "Cabin": { "Cabin": "Y" }, "SeatsRemaining": { "BelowMin": false, "Number": 7 } }, "FareReference": "B" }, { "TPA_Extensions": { "Cabin": { "Cabin": "Y" }, "SeatsRemaining": { "BelowMin": false, "Number": 7 } }, "FareReference": "B" }] }, "TPA_Extensions": { "DivideInParty": { "Indicator": false } }, "ItinTotalFare": { "FareConstruction": { "CurrencyCode": "USD", "DecimalPlaces": 2, "Amount": "249.30" }, "TotalFare": { "CurrencyCode": "USD", "DecimalPlaces": 2, "Amount": "296.40" }, "Taxes": { "Tax": [{ "CurrencyCode": "USD", "DecimalPlaces": 2, "TaxCode": "TOTALTAX", "Amount": "47.10" }] }, "BaseFare": { "CurrencyCode": "USD", "DecimalPlaces": 2, "Amount": "249.30" }, "EquivFare": { "CurrencyCode": "USD", "DecimalPlaces": 2, "Amount": "249.30" } } }, } 返回的列表中,第一个将在第二个之前。例如:

&#13;
&#13;
querySelectorAll
&#13;
var list = document.querySelectorAll("#container div");
var n = 0;
setInterval(function() {
  list[n].classList.remove("highlight");
  n = n == 0 ? 1 : 0;
  list[n].classList.add("highlight");
}, 500);
&#13;
.highlight {
  color: blue;
}
&#13;
&#13;
&#13;

答案 1 :(得分:0)

您可以使用:

  • id selectors
  • 班级选择器
  • 像jQuery这样的库..

但是如果你不想修改html,你甚至可以选择样式属性,(不是最好的方法......)

&#13;
&#13;
var elms = document.querySelectorAll('*[style="width:200px; height:200px;"]');
elms[0].style.background='red';
&#13;
7
&#13;
<!-- div #1 -->
  <div style="width:200px; height:200px;">testing 1 2 3</div>
  <!-- div #2-->
  <div style="width:200px; height:200px;">testing 1 2 3</div>
&#13;
&#13;
&#13;