2014-01-21 32 views
-1

這裏是我的html代碼我想改變的如何更改element.style的樣式?使用Javascript

<img src="linkremoved" style=" 
    height:45px; 
    width:45px; 
    margin-right:10px; 
    -webkit-border-radius: 50%; 
    -moz-border-radius: 50%; 
    border-radius: 50%;"> 

我的風格似乎無法想出解決辦法。我需要在一個用戶腳本的形式。

+1

你試過什麼樣的東西? –

回答

2

使用純JavaScript,最好增加一個ID:

<img id="myImage" src="linkremoved" style="height:45px;width:45px;margin-right:10px;-webkit-border-radius: 50%;-moz-border-radius: 50%;border-radius: 50%;"> 

,然後使用:

document.getElementById('myImage').style.height='50px'; 

如果您不能添加一個id,你需要:

/** 
* Get an image by it's src. 
* 
* Returns the first image matching the src 
*/ 
function getImageBySrc(src) { 
    var images = document.getElementsByTagName('img'); 
    for (var i in images) { 
     if(images[i].getAttribute('src') == src) { 
      return images[i]; 
     } 
    } 
    return null; 
} 

// useage 
var myImage = getImageBySrc('linkremoved'); 
myImage.style.height='50px'; 
+0

我無法爲此添加Id。這是我的問題。 – Mike

+0

我已經更新了答案以反映這一點。將來你應該在你的問題中提到這種限制。 – thelastshadow

相關問題