简体   繁体   English

将提交/提交事件处理程序附加到哪个元素?

[英]Which element to attach submit/onsubmit event handler to?

What element here is the form object that i can attach a submit/onsubmit eventHandler too?这里的什么元素是表格 object 我也可以附加提交/提交事件处理程序? I'm making a chrome extension that modifies a website so I don't exactly have access to the direct website code outside of inspect element.我正在制作一个修改网站的 chrome 扩展,因此我无法完全访问检查元素之外的直接网站代码。

Inspect Element Breakdown检查元素分解

<div class="search-inner" role="form">

I assume that its the highlighted one, and I'm using this code to try getting a reaction out of the eventListener, but it doesn't seem to activate.我假设它是突出显示的,我正在使用此代码尝试从 eventListener 中获取反应,但它似乎没有激活。

const form = document.getElementsByClassName("search-inner");
if(form != null && form.length > 0){
    form[0].addEventListener('onsubmit', run);
}

function run(event){
    console.log("hi");
    event.preventDefault();
}

The website does set up the form so that it does get hidden after submission, in case that affects the onsubmit eventHandler.该网站确实设置了表单,以便在提交后隐藏它,以防影响 onsubmit 事件处理程序。

Adding the role attribute to elements only communicates their role to assistive technology, to tell users what they can expect in terms of the element's behaviour, and to add it to document outlines.role属性添加到元素仅将其角色传达给辅助技术,告诉用户他们对元素行为的期望,并将其添加到文档大纲中。 It does not actually change an element's behaviour.它实际上并没有改变元素的行为。

That's why we should rely on semantic HTML elements whenever we can.这就是为什么我们应该尽可能依赖语义 HTML 元素。

If a <form> doesn't have an action attribute, it's quite likely that it never gets submitted, but that its controls trigger changes based on other events.如果<form>没有action属性,它很可能永远不会被提交,但它的控件会根据其他事件触发更改。 This is quite common in today's reactive pages.这在当今的响应式页面中很常见。

So if you actually want to target any form element that induces change to the DOM, it will get complicated, as most of the time they are not correctly grouped, neither with a form, nor with a <fieldset> .因此,如果您确实想要针对任何导致 DOM 更改的表单元素,它会变得复杂,因为大多数时候它们没有正确分组,既没有使用表单,也没有使用<fieldset>

Depending on what you're trying to achieve, you could look for buttons in the form and bind to their click events.根据您要实现的目标,您可以在表单中查找按钮并绑定到它们的点击事件。

// kind of selector you’d need to implement
form, [role="form"], [role="search"], fieldset {
  button, input[type="button"], input[type="submit"] {
  }
}

Unfortunately it is not really possible to check for registered event handlers on elements .不幸的是,实际上不可能检查元素上注册的事件处理程序

So, as @xOxxOm suggested, you might also listen for change events on the document level, maybe as an additional measure to see whether a button click inside the form changed something.因此,正如@xOxxOm 所建议的那样,您还可以在文档级别监听change事件,也许可以作为额外的措施来查看表单内的按钮单击是否更改了某些内容。


Btw, the form from your example is not valid, since it's lacking its obligatory accessible name.顺便说一句,您示例中的表格无效,因为它缺少强制性的可访问名称。

Also, it's a search form, so the search role might be more appropriate, which doesn't require a name, and you might consider it as well for your extension.此外,它是一个搜索表单,因此search角色可能更合适,它不需要名称,您也可以将其用于您的扩展程序。

This is how the example should have been示例应该是这样的

<div class="search-inner" role="form" aria-labelledby="search-legend">
  <fieldset>
    <legend id="search-legend">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM