从刺激控制器中选择特定目标
我正在寻找一种简单而优雅的方式(刺激路),以从目标元素中选择一个特定元素,形成了我的刺激控制器。
例如:我有一个4个链接和4个段落的列表(在控制器的范围内)。单击链接时,我希望与段落一起发生某些事情(例如添加类),该段落与链接具有相同的数据ID。这些段落被定义为刺激目标。
代码: https://jsfiddle.net/nblvafxy/
请参阅此js-fiddle以获取 正在为我提供以下目标元素的访问:
event.currenttarget
- >给出单击元素(链接) - 可以在其中访问数据集,因此可以在其中访问数据集(event.currenttarget.dataset.linkid
)this.elementTarget - >返回第一个元素目标(段落) - 但我无法选择一个特定的目标。
this.ElementTargets
- >返回一个目标元素(段落)的数组 - 但在这里,我不能过滤特定的目标。
对于最新的一个(this.ElementTargets
),我希望,有一个JS方法可以以某种方式从此html元素中选择一个特定元素。
我想避免document.queryselector(...)
作为带有数据ID的元素多次存在(请参阅JS-Fiddle中的第二个列表)。因此,刺激控制器“范围”中的解决方案将是最佳解决方案。
I am looking for a simple and elegant way (a Stimulus-way) to select a specific element from the targets elements form my Stimulus controller.
For example: I have a list of 4 links and 4 paragraphs (both inside the scope of the controller). When clicking the link, I want something to happen (e.g. adding a class) with the paragraph, which has the same data-id as the link. The paragraphs have been defined as Stimulus-Targets.
Please see this JS-Fiddle for a code-example: https://jsfiddle.net/nbLvafxy/
Stimulus is giving me the following accesses to the target elements:
event.currentTarget
-> gives the clicked element (the link) - where its possible to access the dataset and therefore the data-id of the clicked element (event.currentTarget.dataset.linkId
)this.elementTarget
-> returns the first element target (paragraph) - but I cannot select a specific one.this.elementTargets
-> returns an array of target-elements (paragraphs) - but also here, I cannot filter for a specific one.
For the latest one (this.elementTargets
), I would hope, there is a JS method to somehow select a specific element from this array of HTML elements.
I want to avoid document.querySelector(...)
as the element with the data-id could be present on the page multiple times (see second list in the JS-Fiddle). Therefore, a solution within the "scope" of the Stimulus controller would be the best solution.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是我按名称选择目标的方法:
甚至更容易:
Here is my approach to select target by name:
Or even easier:
i虽然这会起作用,但是
我之前曾在同一页面上使用过多个控制器,但我会同意命名约定的评论者之一事情起来。目标都在两侧选择,添加类作品,但似乎可以重新加载或重新连接页面。
哦!!由于#HREF,它正在重新连接!将您的<标签更改为按钮,然后删除HREF,然后效法。
抱歉,
索引的事情会起作用,但是元素和链接的顺序不相同。
因此,由于某种原因,您的HTML
刺激控制器
在HTML中添加到元素,但不更改?(在Safari中,但在Firefox中还可以)
无论如何,没有DOM ID,只有目标。
I though this would work, but it doesn't
I have used multiple controllers on the same page before and it worked, but I'll agree with the one of the commenter that the naming convention seems to screw things up. The targets are selected on both sides, the add class works, but it seem to reload or reconnect the page.
OH!! It's reconnecting because of the # href! Change your <a tags to button and remove the href and it works.
Sorry
The index thing would work, but elements and links are not in same order.
So, your html
Stimulus controller
For some reason the class added to element is in the html, but not change?(in Safari, but ok in firefox)
anyway no DOM id, just targets.
主要思想是控制器是“可重复使用的”。因此,可以创建许多控制器。
而不是:
您应该
在控制器的代码中设置:
The main idea is that the controller is "reusable". So, it's ok to create a number of controllers.
Instead of:
you should set
Then, in controller's code: