html引用外部css时怎么在后面加随机数 href="......css?id=0.11111" 这样的? 行吗IE不行

html-css039

html引用外部css时怎么在后面加随机数 href="......css?id=0.11111" 这样的? 行吗IE不行,第1张

亲测。各种浏览器均没有问题.代码:

在<head></head>之间可以这样写:

<script>

var str='<link href="css.css?'+Math.random()+'" rel="stylesheet" type="text/css"/>'

document.write(str)

</script>

即可。

可以使用Random类,这里我只准备了3种不同的爱心,来看看代码如何实现吧:

//首先定义 3个代表不同爱心的drawable,以及他们的和drawables

private Drawable red

private Drawable yellow

private Drawable blue

private Drawable[] drawables

//接下去我们初始化:

//初始化显示的图片

drawables = new Drawable[3]

red = getResources().getDrawable(R.mipmap.red)

yellow = getResources().getDrawable(R.mipmap.yellow)

blue = getResources().getDrawable(R.mipmap.blue)

//赋值给drawables

drawables[0]=red

drawables[1]=yellow

drawables[2]=blue

这样,下次取值时候只要使用如下代码,就能随机获取到爱心了:

drawables[random.nextInt(3)]//表示0-2的随机数,注意,3是取不到的。