gpt4 book ai didi

javascript - 如何通过 RJS 代理访问数组?

转载 作者:行者123 更新时间:2023-12-02 20:35:49 25 4
gpt4 key购买 nike

在 Rails 的 RJS 适配器中,

page['id'] // $('id')

访问 CSS-id,

page['id'].property // $('id').property

它的一个属性。但是我如何访问数组索引,例如

page.select('ul').value_at(2) // $('id').select('ul')[2]

有没有什么方法可以不写:

page << "$('id').select('ul')[2]"

最佳答案

您无法从 rjs 访问这样的数组,但是其中任何一个都可以:

page['id']['firstChild']['nextSibling']
page['id'].down(2)

生成:

$("id").firstChild.nextSibling;
$("id").down(2);

当然,最好的解决方案是向第二个元素添加一些识别 css 类或元素 id,然后从 rjs 文件中进行选择。

如果您想坚持使用 page.select,您可以像这样实现 value_at (此处称为 pick):

>>> Array.prototype.pick = function(n) { return this[n]; };
function()
>>> ['a', 'b', 'c'].pick(1)
"b"

关于javascript - 如何通过 RJS 代理访问数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3284045/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com