简体   繁体   中英

Change the order of CSS inside of head tag with Javascript

I have CSS order like this:

<link rel="stylesheet" href="style2.css">
<link rel="stylesheet" href="style1.css">

What I want to achieve is order like this:

<link rel="stylesheet" href="style1.css">
<link rel="stylesheet" href="style2.css">

The ideal would be if this can be done when page loads.

I don't see why you need to have to do it with JavaScript, but here's the answer:

$("LINK[rel='stylesheet']")

you can use this jquery code in order to get the all the link tags for stylesheets. Using array manipulation, you could change the order. More info about this specific snippet here

then once you are ready, you could loop over the code. More info about this from here

$('head').append('<link rel="stylesheet" type="text/css" href="lightbox_stylesheet.css">');

obviously, change what you are appending.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM