:href= 拼接无效_无效的代码:href =“#”是什么意思?

:href= 拼接无效

This particular code in an <a> tag is commonly seen in sample code that involves JavaScript. Most commonly you will see it looking something like <a href="#" οnclick="doSomething(); return false"> where the primary purpose of the tag is to provide a link for people to click on to run some JavaScript.

<a>标记中的特定代码通常在涉及JavaScript的示例代码中可见。 最常见的是,您会看到它看起来像<a href="#" οnclick="doSomething(); return false"> ,其中标记的主要目的是为人们提供一个链接,供其单击以运行一些JavaScript。

When used in sample code like that the # is a place holder representing where ever you want the link to actually go if the person visiting your page does not have JavaScript enabled.

当在示例代码中使用时,#是一个占位符,表示如果访问您的页面的人未启用JavaScript,则您希望链接实际到达的位置。

When you see href="#" in live code on a web page it means that the person who wrote the page has made a mistake. You should never see href="#" in the actual source code of a web page because the # by itself is actually invalid and meaningless.

当您在网页上的实时代码中看到href =“#”时,表示编写该页面的人犯了一个错误。 在网页的实际源代码中,您永远都不会看到href =“#”,因为#本身实际上是无效且毫无意义的。

Whenever you attach JavaScript to a link whether like that or using an unobtrusive equivalent, you always need to also consider those who for whatever reason do not have JavaScript enabled. The return false on the end of my above example prevents the href actually being used if the JavaScript runs but the href is still what will be used if for any reason the JavaScript doesn't run. The href therefore needs to contain an actual valid value based on where you want the link to take people who do not have JavaScript available. Since the person who wrote the JavaScript for you doesn't know where you want those people to be taken they have just inserted a # in their code where you need to substitute the real address.

每当您将JavaScript附加到链接时(无论是这样还是使用不显眼的等效项),都始终需要考虑那些出于某种原因未启用JavaScript的用户。 我上面的示例结尾处的return false阻止了在运行JavaScript的情况下实际上使用href,但是如果出于任何原因JavaScript无法运行,则href仍然会被使用。 因此,href需要包含一个实际有效值,该值取决于您希望链接将没有JavaScript可用的用户带到何处。 由于为您编写JavaScript的人不知道您希望将这些人带到何处,因此他们仅在代码中插入一个#来替换实际地址。

A # is valid in an href attribute provided that it isn't the only character in the value. Where the # is followed by additional characters those additional characters are the value of an id attribute elsewhere in the current web page and the page will jump to display the tag containing that id as close as possible to the top of the browser viewport. For example <a href="#here"> will jump to <div id="here"> in the same web page. If you also have a filename preceding the # then the id it will jump to will be within that web page so <href="next.htm#here"> will jump to that id on the next.htm page.

如果#不是值中的唯一字符,则它在href属性中有效。 在#后跟其他字符的情况下,这些其他字符是当前网页其他位置的id属性的值,并且该页面将跳转以显示包含该id的标签,使其尽可能靠近浏览器视口顶部。 例如,<a href="#here">将跳到同一网页中的<div id =“ here”>。 如果您在#号之前还具有文件名,则它将跳转到的ID将在该网页内,因此<href =“ next.htm#here”>将跳转到next.htm页上的该ID。

A # character is not valid as the last character of the href since it implies that you want to jump to an id within the page but the value of the id to jump to hasn't been specified. The action the browser should take in that instance is undefined however most will simply jump back to the top of the current page.

#字符不能用作href的最后一个字符,因为它暗示您要跳转到页面中的id,但是尚未指定要跳转到的id的值。 浏览器在该实例中应采取的动作是不确定的,但是大多数动作只会跳回到当前页面的顶部。

So what do you do if the JavaScript you want to attach is such that there is no alternative for those without JavaScript? Well in that case you don't want those without JavaScript to see the link at all since if it is visible to them then some of them will click on it and you don't have anything that you want it to do for them and that will just be confusing. The solution therefore is to ensure that the link is only visible for those with JavaScript enabled and the way to do that is to add the link into the web page using JavaScript.

那么,如果要附加JavaScript使得没有JavaScriptJavaScript别无选择,该怎么办? 好吧,在这种情况下,您根本不希望那些没有JavaScript的人看到链接,因为如果对他们可见,则其中的一些人将单击它,而您却没有想要它为他们做的任何事情。只会令人困惑。 因此,解决方案是确保仅对启用了JavaScript的用户可见该链接,并且该方法是使用JavaScript将链接添加到网页中。

Only where the <a href="#" οnclick="doSomething(); return false"> is added into the web page using JavaScript can you be sure that everyone clicking on the link will have JavaScript enabled and tat therefore the doSomething() code will run and the href="#" will be ignored. Then and only then does it make any sense whatever to leave the # in that spot in the code since the href attribute is required in order for some browsers to accept the code as a valid link and where you know that the only people to see the link will have JavaScript enabled you also know that no one will therefore ever end up actually being taken to the place the href points to and so it can contain anything at all without it mattering and so # is as good a value as any and is certainly better than href="javascript:" (which is a construct that should never be used regardless of whether anything follows the colon or not).

只有使用JavaScript将<a href="#" οnclick="doSomething(); return false">添加到网页中的情况下,您才能确保每个单击链接的人都启用了JavaScript,因此doSomething()代码将运行,并且href =“#”将被忽略。 然后,直到那时,将#留在代码中的任何位置才有意义,因为href属性是必需的,以便某些浏览器将代码作为有效链接接受,并且您知道只有人可以看到链接将启用JavaScript,您也知道,因此最终没人会真正被带到href指向的位置,因此它可以包含任何东西而没有关系,因此#与任何值一样好,并且肯定是比href =“ javascript:”更好(这是一个无论是否跟随冒号都不应使用的构造)。

翻译自: https://www.thoughtco.com/href-2037524

:href= 拼接无效

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值