Create a W3C validated anchor link with target=“_blank”

前端 未结 11 1762
臣服心动
臣服心动 2021-01-04 22:51

I have the following piece of HTML that creates a new window when clicked:

glide by the people


        
相关标签:
11条回答
  • 2021-01-04 23:30

    Use this doctype:

    <!DOCTYPE html PUBLIC "-//w3c//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhmtl1/DTD/xhmtl1-transitional.dtd"> 
    

    1.0 transitional accommodates some html "legacy" code, including target="_blank".

    0 讨论(0)
  • 2021-01-04 23:33

    If it's just one or two links, you can do it inline with

    <a href="http://stackoverflow.com" onclick="window.open(this.href); return false;"></a>
    

    More than that and you probably want one of the js solutions above.

    0 讨论(0)
  • 2021-01-04 23:33

    Instead of using:

    target="_blank"

    use:

    onclick="this.target='_blank'"

    Note the single quotes inside of the double quotes. This solved the validation problem for me, without having to redefine the doctype.

    0 讨论(0)
  • 2021-01-04 23:41

    Assuming strict XHTML, you would bind an onclick event to the anchor in question. Something like:

    <a href="/path/to/my/link" onclick="window.open('/path/to/my/link');return false;">My link</a>
    

    One would also argue that you should be binding that onclick action separately with external JavaScript due to progressive enhancement and separating behavior from your markup, but that's the basic way to go about it.

    0 讨论(0)
  • 2021-01-04 23:43
    <a href="..." onclick="return !window.open(this.href)">...</a>
    
    0 讨论(0)
提交回复
热议问题