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

前端 未结 11 1775
臣服心动
臣服心动 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:21

    You can not do it with W3C strict validation.

    The solutions are:

    • Transitional doctype as already mentioned

    • A different validator (e.g. CSE)

    • Use JavaScript to replace "_blank" - see http://www.ajaxblender.com/open-links-new-window-w3c-valid-target-blank.html (the link shows how to do it real neatly with jQuery - I am pasting the 2 examples below).

        $(document).ready(function(){
              $('A[rel="_blank"]').each(function() {
                   $(this).attr('target', '_blank');
              });
         });
         // ...OR...
         $(document).ready(function(){
              $('A[rel="_blank"]').click(function() {
                   window.open($(this).attr('href'));
                   return false;
              });
         });
    
    • Also, as per this doctype page, HTML5 should allow target atrtribute to be validated, fow what it's worth.

提交回复
热议问题