是否有“默认操作”的标准资源? HTML 元素?

发布于 2024-07-19 08:23:55 字数 1542 浏览 5 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

无名指的心愿 2024-07-26 08:23:55

相关文档是 DOM3 EventsHTML 5 规范

他们可能没有您需要的所有信息,但应该有。 因此,如果您发现缺少这些行为,请请求指定这些行为

The relevant documents are DOM3 Events and the HTML 5 specification.

They might not have all information you need, but should. So if you find them lacking, please request those behaviors to be specified.

倒数 2024-07-26 08:23:55

为什么要使用“提交”按钮? 为什么不只使用一个按钮呢?

或者我错过了某物?

why use a Submit button at all? why not just use a button? <input type="button" onclick="function(){if(allCriteriaIsMet()) this.form.submit()}" value="click me"/>

or am I missing something?

各自安好 2024-07-26 08:23:55

您可能很难找到此规范的原因之一是您正在考虑不止一种行为。

设置 onclick="return false;" 将阻止提交按钮提交表单,但不会阻止表单被提交。 如果在表单中的某个字段具有焦点时按 Enter 键,则可能会提交表单本身,而无需使用提交按钮。 (我不知道确切的细节,而且不同浏览器之间的差异可能会有所不同,但您会看到原理。)

One reason that you may have a hard time to find a spec for this is that you are looking at more than one behavior.

Setting onclick="return false;" will keep the submit button from submitting the form, but it will not keep the form from being submitted. If you press enter when a field in the form has focus, the form itself may be submitted without involving the submit button. (I don't know the exact details, and they may vary a bit from one browser to the next, but you see the principle.)

2024-07-26 08:23:55

对于该特定场景,您可能想要尝试不同的事件属性,因为浏览器的行为不同,onlick 用于按钮,onsubmit 是表单事件,所以自然地,如果您为 onclick 返回 false,这并不意味着on commit 不会生效,您应该单独处理 onsubmit,或者将提交委托给提交按钮(通过返回 false onsubmit,并让 onclick 处理程序具有 form.submit),也可以尝试使用多个提交按钮来了解如何它的工作原理

我知道你可能已经知道这一点,但我在处理表单时学到了一些其他技巧,尝试使用 event.cancelBubble = true 和 event.returnValue = false (特定于 IE,不确定 firefox),有时返回 false 是不够好...

至于文档,MSDN 库记录了所有事件的默认操作,这里是提交时的文档:
http://msdn.microsoft.com/en-我们/library/ms535249(VS.85).aspx#

有趣的是:提交方法确实
不调用 onsubmit 事件处理程序。

这太棒了,否则它就会陷入无限循环!

这是 onlcick 的文档
http://msdn.microsoft.com/en-us /library/ms536913(VS.85).aspx

MSDN 库还记录了所有事件和对象,但要付出一定的代价,所有 IE 都特定于此!

for that particular scenario, you might want to experiment with different event properties, as browsers act differently, the onlick is for the button, the onsubmit is a form event, so naturally, if you return false for the onclick, that does not mean the on submit is not going to take effect, you should handle the onsubmit separately, or delegate submission to the submit button (by return false onsubmit, and let the onclick handler have form.submit), also try it with multiple submit buttons to understand how it works

I know you might already know this, but some other tips I picked up when dealing with forms, experiment with event.cancelBubble = true, and event.returnValue = false (IE specific, not sure about firefox), sometimes return false is not good enough...

as for documentation, MSDN library documents the default action for all events, here is the documentation for the on submit:
http://msdn.microsoft.com/en-us/library/ms535249(VS.85).aspx#

Interestingly: The submit method does
not invoke the onsubmit event handler.

which is great, otherwise it would have gone in an infinite loop!

and here is the documentation for the onlcick
http://msdn.microsoft.com/en-us/library/ms536913(VS.85).aspx

MSDN library documents all events and objects as well, but for a price, all IE specific!

久伴你 2024-07-26 08:23:55

是的,您可以设置一个带有侦听器的按钮,当单击该按钮时,您可以停止提交表单。 表单的提交是表单中类型提交(重要)的“按钮”的默认行为。

事件方法 PreventDefault(或者对于窗口事件来说 returnValue=false,具体取决于浏览器遵循的事件模型)将阻止默认的提交行为。 我还停止传播(MSIE evt.model = cancelBubble)。

例如,对于以下类型为“提交”的按钮:

<button type="submit" id="logout" name="logout" value="Log Out">Log Out</button>

为该特定按钮添加 x 浏览器事件侦听器。 例如,假设 testlogout 是控制单击按钮时发生的情况的函数的名称,则可以使用类似以下内容来设置侦听器:

var logoutBtn = (document.getElementById && document.getElementById('logout')) || 
   (document.all && document.all['logout']) || document.forms[formname].elements['logout'] || null;
if (logoutBtn) {
   if (logoutBtn.addEventListener) {
       logoutBtn.addEventListener('click',testlogout,false);
   } else if (window.attachEvent) {
       logoutBtn.attachEvent('onclick',testlogout);
   } else {
       //legacy:
       logoutBtn.onclick = testlogout;
       //...logic for document.layers support, etc.
   }
}

然后在 testlogout 函数中,该函数将自动传递事件,您会测试任何条件。 根据您设置的条件,使用 event.preventDefault 或 returnValue,具体取决于浏览器支持的类型。 它们取代内联“return false”。 例如:

function testlogout(e) 
{
    e = e || window.event;
    //... your logic here...
    e.stopPropagation?e. stopPropagation():(e.cancelBubble?e.cancelBubble():"");
    e.preventDefault?e.preventDefault():e.returnValue=false;
    return false; // in case all else fails?
}

传递的事件当然有其自己的一组属性,其名称也有所不同,具体取决于浏览器遵循的模型。 例如,这些属性将识别单击的目标按钮。

当前的事件侦听模型关注“返回 false”。 可以这么说,“返回 false”命令将被置若罔闻。 但不用担心,只要您使用他们理解的内容即可。

希望这有助于解决当前的根本问题。 阅读 DOM 事件和事件模型将阐明可用的方法和属性。

Yes, you can set up a button-- with a listener, where when the button is clicked, you can stop the submission of the form. The submission of the form is the default behavior for a 'button' of type submit (important) in a form.

The event method, preventDefault (or returnValue=false for the window event, depending on the event model the browser follows), will prevent that default, submission behavior. I also stopPropagation (MSIE evt. model = cancelBubble).

For example, for the following button of type 'submit':

<button type="submit" id="logout" name="logout" value="Log Out">Log Out</button>

Add an x-browser event listener for that particular button. For example, assuming that testlogout is the name of your function that controls what happens when the button is clicked, something like the following might be used to set up the listener:

var logoutBtn = (document.getElementById && document.getElementById('logout')) || 
   (document.all && document.all['logout']) || document.forms[formname].elements['logout'] || null;
if (logoutBtn) {
   if (logoutBtn.addEventListener) {
       logoutBtn.addEventListener('click',testlogout,false);
   } else if (window.attachEvent) {
       logoutBtn.attachEvent('onclick',testlogout);
   } else {
       //legacy:
       logoutBtn.onclick = testlogout;
       //...logic for document.layers support, etc.
   }
}

Then in the testlogout function, which will automatically be passed the event, you would test whatever conditions. Upon the conditions you set, use the event.preventDefault or returnValue, depending again on which the browser supports. These take the place of inline 'return false'. For example:

function testlogout(e) 
{
    e = e || window.event;
    //... your logic here...
    e.stopPropagation?e. stopPropagation():(e.cancelBubble?e.cancelBubble():"");
    e.preventDefault?e.preventDefault():e.returnValue=false;
    return false; // in case all else fails?
}

The event passed has its own set of properties of course, whose names vary, again, depending on the model the browser follows. These properties will identify the target button clicked, for example.

The current event listening models do not pay attention to 'return false'. A 'return false' command will fall on deaf ears, so to speak. But no worries, as long as you use what they do understand.

Hope that helps solve the present, underlying problem. Reading up on DOM Events and event models will clarify the methods and properties available.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文