admin管理员组文章数量:1379719
There are next
and prev
buttons in the slideshow, that I want they move a few pixels and change their colors after clicking on these buttons. The button returns to the normal position by removing the mouse. Although I know I can coding with CSS, but it is part of my homework and I have to do that with javascript. This is my code but it doesn't work and I don't know why ...
function press() {
document.getElementById(this).style.right = "-10px";
document.getElementById(this).style.backgroundColor = "red";
}
<a class="prev" onclick="plusSlides(1)" onmousedown="press()">❮</a>
<a class="next" onclick="plusSlides(-1)" onmousedown="press()">❯</a>
There are next
and prev
buttons in the slideshow, that I want they move a few pixels and change their colors after clicking on these buttons. The button returns to the normal position by removing the mouse. Although I know I can coding with CSS, but it is part of my homework and I have to do that with javascript. This is my code but it doesn't work and I don't know why ...
function press() {
document.getElementById(this).style.right = "-10px";
document.getElementById(this).style.backgroundColor = "red";
}
<a class="prev" onclick="plusSlides(1)" onmousedown="press()">❮</a>
<a class="next" onclick="plusSlides(-1)" onmousedown="press()">❯</a>
Share
Improve this question
edited Jul 26, 2018 at 12:53
eisbehr
12.5k7 gold badges41 silver badges65 bronze badges
asked Jul 26, 2018 at 12:40
mahroomahroo
671 silver badge6 bronze badges
2
- is jQuery available? – Aayush Sharma Commented Jul 26, 2018 at 12:42
-
@mahroo you should pass attribute id value to the
getElementById()
notthis
– Sivakumar Tadisetti Commented Jul 26, 2018 at 12:43
3 Answers
Reset to default 4You need to pass this
to the function, then you have the element directly.
function press(element) {
element.style.marginRight = "-10px";
element.style.backgroundColor = "red";
}
<a class="prev" onmousedown="press(this)">❮</a>
<a class="next" onmousedown="press(this)">❯</a>
function applyStyles(element) {
element.style.backgroundColor = 'red';
element.style.fontSize = '18px'
}
function revertStyles(element) {
element.style.backgroundColor = 'white';
element.style.fontSize = '15px'
}
<span onclick="applyStyles(this)" onmouseleave="revertStyles(this)">❮</span>
<span onclick="applyStyles(this)" onmouseleave="revertStyles(this)">❯</span>
The native function getElementById
only accepts a string as parameter, that must be the "id" of the html element you want to iterate with. Your code should be something like this:
function press(id){
document.getElementById(id).style.right="-10px";
document.getElementById(id).style.backgroundColor="red";
}
<a id="prev" class="prev" onmousedown="press(this.id)" >❮</a>
<a id="next" class="next" onmousedown="press(this.id)">❯</a>
You too can make it directly changing only your js function by passing this as function parameter:
function press(htmlElement){
htmlElement.style.marginRight="-10px";
htmlElement.style.backgroundColor="red";
}
本文标签: htmlmoving button in javascriptStack Overflow
版权声明:本文标题:html - moving button in javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744415504a2605173.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论