// option 1 create within function function increaseSize() { var myImg = document.getElementById( "myImg" ); myImg.style.width = input.value; myImg.style.height = input.value; } // option 2 pass as parameters var myImg = document.getElementById( "myImg" ); // assign the variables var input = document.getElementById( "size" ); function increaseSize( img,input ) { // create the function img.style.width = input.value + 'px'; // assign a unit type to it,as it is a css value img.style.height = input.value + 'px'; } increaseSize( myImg ); // run the function,passing in our variables
var myImg = document.getElementById("myImg") var input = document.getElementById("insert") function increaseSize() { myImg.style.width = input.value + 'px'; myImg.style.height = input.value + 'px'; } function doubleSize() { myImg.style.width = Number(myImg.style.width.slice(0,-2)) * 2 + 'px'; myImg.style.height = Number(myImg.style.height.slice(0,-2)) * 2 + 'px'; }
,
尝试一下
function changeSize(){ console.log('Clikced change size!'); var size=parseInt(document.querySelector('#img_size').value); var img=document.querySelector('#img'); img.Clikced double size!'); var img=document.querySelector('#img'); var size=parseInt(img.width)*2; img.http://placehold.it/120x120&text=image1' id="img">