How do I add target=“_blank” to a link within a specified div?

前端 未结 7 1628
失恋的感觉
失恋的感觉 2020-11-27 03:29

Let\'s say I have the following code:


      
7条回答
  •  余生分开走
    2020-11-27 03:40

    /* here are two different ways to do this */
    //using jquery:
    $(document).ready(function(){
      $('#link_other a').attr('target', '_blank');
    });
    
    // not using jquery
    window.onload = function(){
      var anchors = document.getElementById('link_other').getElementsByTagName('a');
      for (var i=0; i

    You could also add a title tag to notify the user that you are doing this, to warn them, because as has been pointed out, it's not what users expect:

    $('#link_other a').attr('target', '_blank').attr('title','This link will open in a new window.');
    

提交回复
热议问题