Is there a standard resource for the “default action” of HTML elements? [closed]

寵の児 提交于 2019-12-05 16:31:33

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.

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.)

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!

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.

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?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!