Add target=“_blank” in CSS

前端 未结 6 1875
闹比i
闹比i 2020-11-28 07:57

I have external links in top menu of my website. I want to open these links in new tab. I could achieve it using \"target=_blank\" in HTML. Is there a similar CSS p

6条回答
  •  旧巷少年郎
    2020-11-28 08:19

    This is actually javascript but related/relevant because .querySelectorAll targets by CSS syntax:

    var i_will_target_self = document.querySelectorAll("ul.menu li a#example")
    

    this example uses css to target links in a menu with id = "example"

    that creates a variable which is a collection of the elements we want to change, but we still have actually change them by setting the new target ("_blank"):

    for (var i = 0; i < 5; i++) {
    i_will_target_self[i].target = "_blank";
    }
    

    That code assumes that there are 5 or less elements. That can be changed easily by changing the phrase "i < 5."

    read more here: http://xahlee.info/js/js_get_elements.html

提交回复
热议问题