简体   繁体   English

HTML的多个内联注释

[英]Multiple inline comments for html

I am trying to comment the following html so that it shouldn't go to hyperlink. 我正在尝试评论以下html,以便它不应转到超链接。

<a href="<!--http://www.google.com=-->" target="_blank" onclick="javascript:alert('Navigation Prevented')">CLICK HERE FOR GOOGLE</a>

I am unable to comment target="_blank". 我无法评论target =“ _ blank”。 if I do so it doesn't work and it displays javascript on page as well. 如果这样做,它将无法正常运行,并且也会在页面上显示javascript。

How do I have the page display alert as well as stop opening the page in new tab. 如何使页面显示警报以及如何在新选项卡中停止打开页面。 I want the effect of target="_blank" to be cancelled. 我希望取消target =“ _ blank”的效果。

Can't we have multiple comments inline for html. 我们不能为html内联多个注释。

You cannot have comment tags inside the attribute value of an element. 您不能在元素的属性值中包含注释标签。

Instead of commenting, you can do this way, by adding a return false; 您可以通过添加return false;代替注释return false; . This prevents the browser from following the link: 这样可以防止浏览器跟踪链接:

<a href="http://www.google.com" target="_blank"
   onclick="javascript:alert('Navigation Prevented'); return false;">CLICK HERE FOR GOOGLE</a>

Fiddle: http://jsfiddle.net/rBmjs/ 小提琴: http//jsfiddle.net/rBmjs/

<a href="google.com" target="_blank" onclick="javascript:alert('Navigation Prevented');return false;">CLICK HERE FOR GOOGLE</a>

The important part of this is the return false; 重要的是return false; on the onclick attribute. onclick属性上。 This instructs the browser to immediately cancel this link after clicking. 这指示浏览器单击后立即取消此链接。 So what will happen is the javascript will be executed, but the link will not be followed. 因此,将执行javascript,但不会遵循该链接。

You can't have HTML comments inside an HTML tag, as the comment is a tag itself. HTML标记内不能包含HTML注释,因为注释本身就是标记。

You can return false from the event handler to keep the browser from following the link: 您可以从事件处理程序中返回false ,以防止浏览器跟踪该链接:

<a href="http://www.google.com" target="_blank" onclick="javascript:alert('Navigation Prevented');return false;">CLICK HERE FOR GOOGLE</a>

After seeing other users say that comments can't be included within element attributes, I started to wonder why. 在看到其他用户说注释不能包含在元素属性中之后,我开始想知道为什么。 Certainly it's a bad practice, but why shouldn't it work? 当然,这是一个不好的做法,但是为什么不起作用呢?

I checked the specs for HTML5 comments and HTML4.01 comments , and the answer was in the 4.01 spec: 我检查了HTML5注释HTML4.01注释的规范,答案在4.01规范中:

Note that comments are markup 请注意,注释是标记

The <! <! part of a comment merely opens a declaration, and > closes it. 注释的一部分仅打开一个声明,然后>关闭。 It's the -- string that identifies the declaration as a comment. --字符串将声明标识为注释。 This becomes obvious when comparing comment syntax to doctype declarations and CDATA sections. 将注释语法与doctype声明和CDATA节进行比较时,这一点变得显而易见。

Because you can't put declarations inside attribute values (I wasn't able to find this explicitly stated in the spec, but it seems obvious), comments can't be included in attribute values. 因为您不能在属性值内放置声明(我无法在规范中找到明确声明的内容,但似乎很明显),因此注释不能包含在属性值中。

Interestingly, the HTML5 comments section doesn't mention the 'comments are markup' note. 有趣的是, HTML5注释部分没有提到“注释是标记”注释。 However, I feel sure the same rules still hold. 但是,我认为相同的规则仍然适用。

If any of this is wrong, please feel free to post corrections. 如果有任何错误,请随时发布更正。

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

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