无效的 html(href 目标标记)

发布于 2024-08-03 10:14:28 字数 435 浏览 3 评论 0原文

我很好奇这应该如何正确完成。

我通过 w3 html 验证器运行了一个正在处理的页面,并收到一条错误消息

第 47 行第 54 列:属性“目标”存在,但不能用于此元素。**

<ul><li><a href="./jobops/1000 Design PM.pdf" target="blank">1000 Design PM</a></li>

您已在文档,但您使用的文档类型不支持该元素的该属性。此错误通常是由于错误地将“严格”文档类型与使用框架的文档一起使用(例如,您必须使用“过渡”文档类型来获取“目标”属性),或者使用供应商专有扩展(例如“ marginheight”(这通常通过使用 CSS 来修复以达到所需的效果)。

关于如何让链接打开新窗口但不使用目标标签的任何想法?

I've curious how this should be properly done.

I ran a page im working on through the w3 html validateor and I got one error message

Line 47, Column 54: Attribute "target" exists, but can not be used for this element.**

<ul><li><a href="./jobops/1000 Design PM.pdf" target="blank">1000 Design PM</a></li>

You have used the attribute named above in your document, but the document type you are using does not support that attribute for this element. This error is often caused by incorrect use of the "Strict" document type with a document that uses frames (e.g. you must use the "Transitional" document type to get the "target" attribute), or by using vendor proprietary extensions such as "marginheight" (this is usually fixed by using CSS to achieve the desired effect instead).

any idea on how i can have a link open a new window but not use the target tag?

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

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

发布评论

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

评论(7

挽心 2024-08-10 10:14:28

您可以使用 JavaScript 打开新窗口,这样可以避免目标被删除的问题在现代 HTML 中无效。

然而,这绕过了人们警告他们新窗口(或阻止它们打开)的各种系统,因此您最好使用目标属性(并切换到允许它的 Doctype)。

更好的是让用户决定何时需要新窗口。除了令人烦恼的因素之外,它们还带来了可访问性问题

You can use JavaScript to open new windows, which avoids the issue of target being invalid in modern HTML.

However, this bypasses various systems people have in place to warn them about new windows (or prevent them from opening) so you are better off using the target attribute (and switching to a Doctype that allows it).

Better still is to leave it to the user to decide when they want a new window. Aside from the annoyance factor, they do introduce accessibility problems.

梦幻的心爱 2024-08-10 10:14:28
target="_blank"

不会严格验证,因为“目标”属性已被弃用。

相反,尝试类似于上述 onclick 解决方法的方法,但您也不需要其中的“_blank”。简单地说:

<a href="./jobops/1000 Design PM.pdf" onclick="window.open(this.href); return false;">1000 Design PM</a>

会起作用。弃用“target”的原因是因为 HTML 用于在语义上标记数据,而 target 属性则提供行为,这正是 javascript 的用途。

如果用户关闭了 javascript,则 URL 将在同一窗口中打开。

target="_blank"

Will not validate strict because the 'target' attribute has been deprecated.

Instead, try something similar to the aforementioned onclick workaround, but you don't need the "_blank" in there either. Simply:

<a href="./jobops/1000 Design PM.pdf" onclick="window.open(this.href); return false;">1000 Design PM</a>

Will work. The reason for the deprecation of "target" is because HTML is used to semantically mark up data whereas the target attribute was providing behaviour, which is what javascript is for.

If the user has javascript turned off then the URL will simply open in the same window.

暖阳 2024-08-10 10:14:28

target 属性不属于 HTML 4 和 XHTML 1.0 以及 XHTML 1.1 严格变体的一部分。

因此,您需要使用 JavaScript 来解决问题:

<a href="./jobops/1000 Design PM.pdf" class="_blank">1000 Design PM</a>

var aElems = document.getElementsByTagName("a");
for (var i=0, n=aElems.length; i<n; ++i) {
    if (/(?:^|\s+)_blank(?:\s+|$)/.test(aElems[i].className)) {
        aElems[i].onclick = function() {
            return !window.open(this.href, "_blank");
        }
    }
}

或者(将来)CSS 3(请参阅 超链接呈现模块):

a._blank {
    target: new;
}

The target attribute is not part of the Strict variants of HTML 4 and XHTML 1.0 as well as XHTML 1.1.

So you would need to use a workaround using JavaScript:

<a href="./jobops/1000 Design PM.pdf" class="_blank">1000 Design PM</a>

var aElems = document.getElementsByTagName("a");
for (var i=0, n=aElems.length; i<n; ++i) {
    if (/(?:^|\s+)_blank(?:\s+|$)/.test(aElems[i].className)) {
        aElems[i].onclick = function() {
            return !window.open(this.href, "_blank");
        }
    }
}

Or (in the future) CSS 3 (see Hyperlink Presentation Module):

a._blank {
    target: new;
}
哭泣的笑容 2024-08-10 10:14:28

难道不是吗……

target="_blank"

无论如何……您可以使用 javascript 打开一个新窗口,但这破坏了简单浏览的美感。如果我使用 Lynx 或其他东西浏览怎么办?

Shouldn't it be...

target="_blank"

Regardless... You could open a new window using javascript, but that breaks the beauty of simple browsing. What if I'm browsing using Lynx or something?

尹雨沫 2024-08-10 10:14:28

根据 W3C 的说法,“target”属性在 HTML 5 中似乎不再被弃用:

a 和 area 元素的目标属性不再是
已弃用,因为它在 Web 应用程序中很有用,例如结合使用
使用 iframe。

http://www.w3.org/TR/html5-diff/

According to W3C, it seems that "target" attribute is not deprecated any more in HTML 5:

The target attribute for the a and area elements is no longer
deprecated, as it is useful in Web applications, e.g. in conjunction
with iframe.

http://www.w3.org/TR/html5-diff/

吝吻 2024-08-10 10:14:28

使用:

target="_blank"

破坏严格的 XHTML 验证方法。 这是一份详细说明的文档解决方法:

Using:

target="_blank"

breaks strict XHTML validation methods. Here's a document detailing a workaround:

度的依靠╰つ 2024-08-10 10:14:28

您可以使用这样的 JavaScript,而不是 target="_blank"(永远不会验证):

<a onclick='window.open("./jobops/1000 Design PM.pdf", "_blank");return false;' href="./jobops/1000 Design PM.pdf">1000 Design PM</a>

然后链接将在新窗口中打开,并且您的页面将验证。

未启用 Javascript 的用户(尽管只占所有用户的 2% 左右)仍然可以使用此方法访问链接。这家伙的评论很有道理:)

祝大家周末愉快......

Instead of target="_blank" (who will never validate) you could use javascript like this:

<a onclick='window.open("./jobops/1000 Design PM.pdf", "_blank");return false;' href="./jobops/1000 Design PM.pdf">1000 Design PM</a>

Then the link will open in a new window, and your page will validate.

The users who doesn't have Javascript enabled (even though that is only about 2% of all users), will still be able to follow the link with this method. The guy's have a good point in the comments :)

Have a nice weekend everyone...

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