javascript Javascript更改图像宽度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22798060/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript change width of image onclick
提问by user3487092
I'm a complete noob when it comes to javascript. Would there be anyway to change an image after it is clicked, some way to trigger a js function to change the css. It would have to be triggered by an event and something other than onclick, onfocus probably.
说到 javascript,我完全是个菜鸟。无论如何,在单击图像后是否可以更改图像,以某种方式触发 js 函数来更改 css。它必须由事件和 onclick 以外的其他内容触发,可能是 onfocus。
<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>
回答by kyros
try this...
试试这个...
function mouseOver() {
document.getElementById('image').style.height = "400px";
}
回答by javakorr
Change the JS to this:
将 JS 更改为:
var image = document.getElementById('image');
function mouseOver() {
image.style.height="600px";
}
image.onclick = mouseOver;
回答by Phaedra
Setting values you can use directly style attribute, but remember that asking for them is a greater problem:
设置值可以直接使用 style 属性,但请记住,要求它们是一个更大的问题:
Please refer to this one: Get a CSS value with JavaScript
回答by DcHerrera
This should work
这应该工作
<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'
/>
just copy and edit the image tag code as needed
只需根据需要复制和编辑图像标签代码
回答by ProllyGeek
First i edited the question , because the function was not defined correctly .
首先我编辑了问题,因为函数定义不正确。
Second :
第二 :
to access the height property of any element , you should use style.height
, and should add "px" to the value.
要访问任何元素的高度属性,您应该使用style.height
,并且应该将“px”添加到值中。
please spend more time searching for answers , instead of posting a new question.
请花更多时间寻找答案,而不是发布新问题。