Html XHTML Strict 1.0 - target="_blank" 无效?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4666523/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
XHTML Strict 1.0 - target="_blank" not valid?
提问by Tomkay
I just validated my actual XHTML Strict 1.0 doc with the w3c validator service.. and it says that,
我刚刚使用 w3c 验证器服务验证了我实际的 XHTML Strict 1.0 文档。它说,
<ul id="socialnetwork">
<li><a href="http://www.twitter.com" target="_blank"></a></li>
<li><a href="http://www.flickr.com" target="_blank"></a></li>
<li><a href="http://www.xing.com" target="_blank"></a></li>
<li><a href="http://www.rss.com" target="_blank"></a></li>
</ul>
the target="_blank" is not valid.. but I need the target blank so a new tab will open in the browser, so that the user does not leave the main page.
目标="_blank" 无效.. 但我需要目标空白,以便在浏览器中打开一个新选项卡,以便用户不会离开主页。
What can I do? Why is this not valid?
我能做什么?为什么这是无效的?
采纳答案by Tomkay
You might want to check out the W3 Frequently Asked Questions: http://www.w3.org/MarkUp/2004/xhtml-faq#target
您可能想查看 W3 常见问题解答:http: //www.w3.org/MarkUp/2004/xhtml-faq#target
Why was the target attribute removed from XHTML 1.1?
It wasn't. XHTML 1.0 comes in three versions: strict, transitional, and frameset. All three of these were deliberately kept as close as possible to HTML 4.01 as XML would allow. XHTML 1.1 is an updated version of XHTML 1.0 strict, and no version of HTML strict has ever included the target attribute. The other two versions, transitional and frameset, were not updated, because there was nothing to update. If you want to use the target attribute, use XHTML 1.0 transitional.
为什么要从 XHTML 1.1 中删除 target 属性?
不是。XHTML 1.0 有三个版本:strict、transitional 和 frameset。在 XML 允许的情况下,所有这三个都特意保持尽可能接近 HTML 4.01。XHTML 1.1 是 XHTML 1.0 strict 的更新版本,并且 HTML strict 的任何版本都没有包含 target 属性。另外两个版本transitional和frameset没有更新,因为没什么可更新的。如果要使用目标属性,请使用 XHTML 1.0 过渡。
回答by theking2
The question you should ask yourself is not how to "circumvent" the restriction of Strict but why you want to use XHTML Strict 1.0 in the first place?
您应该问自己的问题不是如何“规避” Strict 的限制,而是首先为什么要使用 XHTML Strict 1.0?
In your case I would simply use Transitional as the DTD. Unless of course you are developing for a specific operating system that for instance doesn't allow multiple windows to be opened for instance in car systems, a mobile phone, or more exotic appliances. Which is, btw, the reason why target is absent in the HTML Strict.
在你的情况下,我会简单地使用 Transitional 作为 DTD。当然,除非您正在为特定的操作系统开发,例如不允许在汽车系统、移动电话或更多奇特的电器中打开多个窗口。顺便说一句,这就是 HTML Strict 中缺少 target 的原因。
But as you seem to develop for "normal" usage your doc type should reflect that and you should be using:
但是当您似乎为“正常”使用而开发时,您的文档类型应该反映这一点,并且您应该使用:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
See also why was target removed from xhtmlcheers J
另请参阅为什么目标从 xhtml欢呼 J 中删除
回答by kevinji
I suggest notadding in the target attribute. It was dropped due to accessibility reasons, and I dislike it when the pagedecides for mehow my browser tags open. Of course, you are free to do so, if you wish to. I will show you a JavaScript method that Darin mentioned above that allows you to validate as XHTML 1.0 Strict or XHTML 1.1:
我建议不要添加目标属性。由于可访问性原因,它被删除了,当页面为我决定浏览器标签的打开方式时,我不喜欢它。当然,如果您愿意,您可以自由地这样做。我将向您展示 Darin 上面提到的一个 JavaScript 方法,它允许您验证为 XHTML 1.0 Strict 或 XHTML 1.1:
HTML code:
HTML代码:
<!-- Added link titles for better testing purposes -->
<ul id="socialnetwork">
<li><a href="http://www.twitter.com/" class="targetblank">Twitter</a></li>
<li><a href="http://www.flickr.com/" class="targetblank">Flickr</a></li>
<li><a href="http://www.xing.com/" class="targetblank">XING</a></li>
<li><a href="http://www.rss.com/" class="targetblank">RSS</a></li>
</ul>
JavaScript code:
JavaScript 代码:
window.onload = function() {
// Code if document.getElementByClassName() doesn't exist
if (document.getElementsByClassName == undefined) {
document.getElementsByClassName = function(className) {
var hasClassName = new RegExp("(?:^|\s)" + className + "(?:$|\s)");
var allElements = document.getElementsByTagName("*");
var results = [];
var element;
for (var i = 0; (element = allElements[i]) != null; i++) {
var elementClass = element.className;
if (elementClass && elementClass.indexOf(className) != -1 && hasClassName.test(elementClass))
results.push(element);
}
return results;
}
}
var anchorList = document.getElementsByClassName('targetblank');
for (var i in anchorList) {
anchorList[i].target = '_blank';
}
}
Of course, you can omit the window.onload if you already include it elsewhere, but I recommend using it (or using another load function, such as JQuery's $(document).ready();
) so the JavaScript loads when the page finishes loading. Now, all you need to do is give each anchor link a class of "targetblank
", and the links should open in a new tab.
当然,如果您已经将 window.onload 包含在其他地方,则可以省略它,但我建议使用它(或使用其他加载函数,例如 JQuery 的$(document).ready();
),以便在页面完成加载时加载 JavaScript。现在,您需要做的就是给每个锚链接一个“ targetblank
”类,链接应该在新选项卡中打开。
回答by Kristian Williams
For this situation I use a simple jQuery solution that validates it with XHTML Strict, and allows new windows to appear.
对于这种情况,我使用了一个简单的 jQuery 解决方案,它使用 XHTML Strict 对其进行验证,并允许出现新窗口。
<a href="http://www.example.com" class="linkExternal">Example URL</a>
<script type="text/javascript">
$(function(){
$('a.linkExternal').on('click',function(e){
e.preventDefault();
window.open($(this).attr('href'));
});
});
回答by Darin Dimitrov
While I cannot say why this attribute is considered invalid as a workaround you could add this attribute with javascript if you want your site to validate as XHTML Strict.
虽然我不能说为什么这个属性被视为无效的解决方法,但如果您希望您的站点验证为 XHTML Strict,您可以使用 javascript 添加此属性。
回答by jagb
The best way to use target in XHTML STRICT is: onclick="target='_blank';"
在 XHTML STRICT 中使用 target 的最佳方法是: onclick="target='_blank';"
<a href="http://botje.tnhteam.com/" onclick="target='_blank';">Botje is overal</a>
Example: click the STRICT button at the bottom
if you need _self or any other target you can change the _blank to _self for example: onclick="target='_self';"
如果您需要 _self 或任何其他目标,您可以将 _blank 更改为 _self 例如: onclick="target='_self';"
I hope this answer is helpful to some of you...
我希望这个答案对你们中的一些人有所帮助......
回答by prplxr
Try this:
尝试这个:
<a href="#" onclick="window.open('urlgoeshere');">Link</a>
回答by Timur Gafforov
I prefer this
我更喜欢这个
<a href="http://myurl.com" onclick="this.target='_blank'">Anchor text</a>