Prototype Js 多重选择器
Prototype Js Multiple Selector
这是我的 css 路径:
#product_composite_configure .entry-edit .buttons-set.a-right .scalable
如何 select 使用原型 js?我想为这个项目添加一个新属性。
这行不通:
<script type="text/javascript">
$$('#product_composite_configure .entry-edit .buttons-set.a-right .scalable').setAttribute("test","test");
</script>
谢谢
[更新]
这也行不通:
$$('div#product_composite_configure div.entry-edit div.buttons-set button.scalable').setAttribute("test","test");
为什么要把事情复杂化?试试这个:
var selectors = ["#product_composite_configure",".entry-edit",".buttons-set",".a-right",".scalable"];
selectors.forEach(function(e, i) {
$$(e).each(function(elem) {
elem.setAttribute("test","test");
})
});
$$()
选择器returns选择元素的数组。因此,您需要遍历数组或使用内置迭代器 invoke()
.
例如
$$('.css_class').invoke('writeAttribute','checked',true);
或您的 CSS 选择器
$$('#product_composite_configure .entry-edit .buttons-set.a-right .scalable').invoke('writeAttribute',"test","test");
我使用 writeAttribute()
而不是 setAttribute()
因为它适用于 PrototypeJS 支持的所有浏览器
还有一些其他方法可以使这项工作正常进行
$('product_composite_configure').down('.entry-edit .buttons-set.a-right .scalable').writeAttribute('test','test');
$$('#product_composite_configure .entry-edit .buttons-set.a-right .scalable')[0].writeAttribute('test','test');
这完全取决于您的 HTML 结构和您想要的行为。如果您只打算定位一个元素,那么使用带有 down()
的 id 查找可能会更好。如果您要在 #product_composite_configure
元素中有多个子元素,那么将 $$()
选择器与 invoke()
一起使用意味着任何新元素也会受到影响,而无需重写脚本。
这是我的 css 路径:
#product_composite_configure .entry-edit .buttons-set.a-right .scalable
如何 select 使用原型 js?我想为这个项目添加一个新属性。
这行不通:
<script type="text/javascript">
$$('#product_composite_configure .entry-edit .buttons-set.a-right .scalable').setAttribute("test","test");
</script>
谢谢
[更新]
这也行不通:
$$('div#product_composite_configure div.entry-edit div.buttons-set button.scalable').setAttribute("test","test");
为什么要把事情复杂化?试试这个:
var selectors = ["#product_composite_configure",".entry-edit",".buttons-set",".a-right",".scalable"];
selectors.forEach(function(e, i) {
$$(e).each(function(elem) {
elem.setAttribute("test","test");
})
});
$$()
选择器returns选择元素的数组。因此,您需要遍历数组或使用内置迭代器 invoke()
.
例如
$$('.css_class').invoke('writeAttribute','checked',true);
或您的 CSS 选择器
$$('#product_composite_configure .entry-edit .buttons-set.a-right .scalable').invoke('writeAttribute',"test","test");
我使用 writeAttribute()
而不是 setAttribute()
因为它适用于 PrototypeJS 支持的所有浏览器
还有一些其他方法可以使这项工作正常进行
$('product_composite_configure').down('.entry-edit .buttons-set.a-right .scalable').writeAttribute('test','test');
$$('#product_composite_configure .entry-edit .buttons-set.a-right .scalable')[0].writeAttribute('test','test');
这完全取决于您的 HTML 结构和您想要的行为。如果您只打算定位一个元素,那么使用带有 down()
的 id 查找可能会更好。如果您要在 #product_composite_configure
元素中有多个子元素,那么将 $$()
选择器与 invoke()
一起使用意味着任何新元素也会受到影响,而无需重写脚本。