2014-04-01 48 views
0

我是一個完整的noob,當涉及到JavaScript。無論如何改變一個圖像後,它被點擊,某種方式來觸發一個JS函數來改變CSS。它必須由一個事件和onclick以外的其他事件觸發,可能是焦點。JavaScript更改寬度圖像onclick

<style> 
    #pic { 
    width:100px; 
    height:100px; 
    } 
</style> 
</head> 
<body> 

<img src='nope.jpg' id='pic' onclick="mouseOver()"></img> 

<script type='text/javascript'> 
    function mouseOver() { 
    document.getElementById('pic').style.width="400px"; 
    document.getElementById('pic').style.height="400px"; 
    }  
</script> 
+0

根據您的例子中,沒有JS需要編輯的圖像標記代碼。看看懸停的css3轉換 – fnostro

+0

它必須是js,但鼠標單擊事件會觸發所有更改。 – user3487092

回答

0

首先,我編輯了這個問題,因爲函數沒有正確定義。

二:

訪問任何元素的高度屬性,你應該使用style.height,並應「PX」添加到值。

請花更多時間尋找答案,而不是發佈一個新問題。

0

試試這個...

function mouseOver() { 
    document.getElementById('image').style.height = "400px"; 
} 
0

更改JS這樣:

var image = document.getElementById('image'); 

function mouseOver() { 
    image.style.height="600px"; 
} 

image.onclick = mouseOver; 
0

這應該工作

<style> 
#pic { 
width:100px; 
height:100px; 
} 
</style> 
</head> 
<body> 
<img 
     width="100" 
     onmouseOver="this.width=400; this.height=400" 
     onclick="this.width=100" 
     alt="RESIZE IMAGE" 
     id='pic' 
     src='nope.jpg' 
    /> 

只是複製,並根據需要