Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 原型Js多重选择器_Javascript_Prototypejs - Fatal编程技术网

Javascript 原型Js多重选择器

Javascript 原型Js多重选择器,javascript,prototypejs,Javascript,Prototypejs,这是我的css路径: #product_composite_configure .entry-edit .buttons-set.a-right .scalable 如何使用prototype js选择此选项?我想向该项添加一个新属性 这不起作用: <script type="text/javascript"> $$('#product_composite_configure .entry-edit .buttons-set.a-right .scalable').setA

这是我的css路径:

#product_composite_configure .entry-edit .buttons-set.a-right .scalable
如何使用prototype js选择此选项?我想向该项添加一个新属性

这不起作用:

<script type="text/javascript">
    $$('#product_composite_configure .entry-edit .buttons-set.a-right .scalable').setAttribute("test","test");
</script>

为什么要把事情复杂化?试试这个:

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");
   })
});

为什么要把事情复杂化?试试这个:

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");
   })
});
$$()
选择器返回所选元素的数组。因此,您需要迭代数组或使用内置迭代器
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\u composite\u configure
元素中有多个子元素,则将
$$()
选择器与
invoke()
一起使用,意味着任何新元素都会受到影响,而无需重写脚本。

$$()
选择器将返回所选元素的数组。因此,您需要迭代数组或使用内置迭代器
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\u composite\u configure
元素中有多个子元素,那么将
$$()
选择器与
invoke()
一起使用意味着任何新元素都会受到影响,而不必重写脚本。

首先,为什么要使用prototype?它已经快一年没有承诺了。。。这在JavaScript领域是一段漫长的时光……我必须:(这不是我的choice@JaredSmith也许它只是达到了最佳状态,没有任何改进-P@Bergi你的意思是像常见的Lisp规范一样?:-PFirst,你为什么要使用prototype?它已经快一年没有提交了…这在JavaScript领域是一段漫长的时间…我必须:(这不是我的choice@JaredSmith也许它只是达到了最佳状态,没有任何改进-P@Bergi你的意思是像常见的Lisp规范一样?:-PI在firebug:TypeError:$$(…).setAttribute不是一个函数var BundleControl=Class.create();Do
console.log(e)
我不熟悉prototype btwI在firebug:TypeError:$$(…)。setAttribute不是一个函数var BundleControl=Class.create();Do
console.log(e)
,我不熟悉prototype btwThx,因为它与我们共享此信息:)与我们共享此信息的Thx:)