动作之间的差异=“”和动作=“#”在HTML中 [英] Difference between action="" and action="#" in HTML

查看:187
本文介绍了动作之间的差异=“”和动作=“#”在HTML中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到了设置表单的action属性的两种方法。



#1。一个空的动作属性:

  action =
pre>

#2。使用的动作属性:
$ b

  action =# 

两者之间有什么区别?

)解析为基本URL,第二个(



以下内容完全有效:



<$>
)解析为文档网址。 p $ p> < form action =method =post>
< p>< input type =submit/>< / p>
< / form>

现在请注意,根据 HTML4规范 action 属性是强制性的,它必须包含有效的URI。但根据 URI RFC ,一个空的URI仍然是一个URI:


不包含URI的URI引用是对当前文档的引用。换句话说,文档中的空白URI引用被解释为对该文档开始的引用,并且仅包含片段标识符的引用是对该文档的标识片段的引用。遍历这样的引用不应该导致额外的检索操作。但是,如果URI引用出现在始终用于产生新请求的上下文中(如HTML的FORM元素),那么空的URI引用表示当前文档的基本URI,应该由该URI替换当转换成请求时。

(摘自






尽管它是强制性的,但如果没有指定操作属性,大多数(如果不是所有的话)浏览器都会回发给响应的发起者。

在HTML5中,动作属性不是必需的。来自规格


动作和formaction内容属性如果指定,必须具有一个值,该值是可能包围的有效非空网址


相关:


  • < a href =https://stackoverflow.com/questions/1131781/is-it-a-good-practice-to-use-an-empty-url-for-a-html-forms-action-attribute-a? rq = 1>对HTML表单的action属性使用空的URL是一种好的做法吗? (action ="")


I've seen two methods of setting a form's action attribute.

#1. An empty action attribute:

action=""

#2. Action attribute with #:

action="#"

What are the differences between the two?

解决方案

The first one ("") resolves to the base URL, and the second one (#) resolves to the document URL.

The following is perfectly valid:

<form action="" method="post">
    <p><input type="submit"/></p>
</form>

Now beware, according to the HTML4 specification, the action attribute is mandatory, and it must contain a valid URI. But according to the URI RFC, an empty URI is still a URI:

A URI reference that does not contain a URI is a reference to the current document. In other words, an empty URI reference within a document is interpreted as a reference to the start of that document, and a reference containing only a fragment identifier is a reference to the identified fragment of that document. Traversal of such a reference should not result in an additional retrieval action. However, if the URI reference occurs in a context that is always intended to result in a new request, as in the case of HTML's FORM element, then an empty URI reference represents the base URI of the current document and should be replaced by that URI when transformed into a request.

(extract from this page)


Although it is mandatory, most if not all browsers will post back to the originator of the response if no action attribute is specified.

And in HTML5, the action attribute is not mandatory. From the specs:

The action and formaction content attributes, if specified, must have a value that is a valid non-empty URL potentially surrounded by spaces.

Related:

这篇关于动作之间的差异=“”和动作=“#”在HTML中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆