簡體   English   中英

querySelectorAll - 如何只獲取某個div中的元素

[英]querySelectorAll - How to only get elements within a certain div

我想限制這個選擇器,只給我一個id為“divId1”的div里面的結果

var polys = document.querySelectorAll('polygon,polyline');

我怎樣才能做到這一點?

嘗試空間運算符:

var polys = document.querySelectorAll('#divId1 polygon, #divId1 polyline');

有關選擇器的信息請參閱MDN的頁面以了解更多信息。

var polys = document.querySelectorAll('#divId1 polygon, #divId1 polyline'); 

嘗試這個:

var el = document.querySelector('#divId1');
var polys = el.querySelectorAll('polygon,polyline');

https://developer.mozilla.org/en-US/docs/Web/API/element.querySelectorAll

您可以在div元素而不是文檔上調用querySelectorAll。

參考: https//developer.mozilla.org/en-US/docs/Web/API/Element.querySelectorAll

樣品:

var polys = document.getElementById('divId1').querySelectorAll('polygon,polyline');

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM