6

我想限制这个选择器,只给我一个 id 为“divId1”的 div 内部的结果

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

我怎样才能做到这一点?

4

4 回答 4

10

Try the space operator:

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

See MDN's page on selectors to learn more.

于 2013-08-23T13:35:35.880 回答
7

您可以在 div 元素而不是文档上调用 querySelectorAll。

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

样本:

var polys = document.getElementById('divId1').querySelectorAll('polygon,polyline');
于 2013-08-23T13:38:58.043 回答
5

尝试这个:

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

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

于 2013-08-23T13:38:09.523 回答
4
var polys = document.querySelectorAll('#divId1 polygon, #divId1 polyline'); 
于 2013-08-23T13:36:18.023 回答