我想限制这个选择器,只给我一个 id 为“divId1”的 div 内部的结果
var polys = document.querySelectorAll('polygon,polyline');
我怎样才能做到这一点?
我想限制这个选择器,只给我一个 id 为“divId1”的 div 内部的结果
var polys = document.querySelectorAll('polygon,polyline');
我怎样才能做到这一点?
Try the space operator:
var polys = document.querySelectorAll('#divId1 polygon, #divId1 polyline');
See MDN's page on selectors to learn more.
您可以在 div 元素而不是文档上调用 querySelectorAll。
参考:https ://developer.mozilla.org/en-US/docs/Web/API/Element.querySelectorAll
样本:
var polys = document.getElementById('divId1').querySelectorAll('polygon,polyline');
尝试这个:
var el = document.querySelector('#divId1');
var polys = el.querySelectorAll('polygon,polyline');
https://developer.mozilla.org/en-US/docs/Web/API/element.querySelectorAll
var polys = document.querySelectorAll('#divId1 polygon, #divId1 polyline');