-2

我想根据 URL 用 javascript 更改图像的大小,

我的意思是,如果网址是:

mywebsite.com/something.hmtl?width=400&height=250

图像为 400x250

如果 URL 是

mywebsite.com/something.hmtl?width=300&height=150

图像尺寸为 300x150

我怎么能用 html 和 javascript 做到这一点?多谢

4

1 回答 1

2

您可以通过以下方式获取参数:

 params = location.search.substr(1).split('&')
 dimentions = {}
 for ( i in params){
   attr = params[i].split('=')
   if(attr[0] == 'width' || attr[0] == 'height'){ 
        dimentions[attr[0]] = parseInt(attr[1] )
     }
  }
 img =  document.getElementsByTagName('img')[0] // will get you the first img of the page
 img.setAttribute('width',dimentions['width'])
 img.setAttribute('height',dimentions['height'])
于 2013-06-23T01:48:52.377 回答