[英]change the position of div tag dyanmically with javascript
I have an ASP button in a div
to the right side of a page.我在页面右侧的
div
中有一个 ASP 按钮。 I want to change the position to the left in the same row dynamically with onchange
event of a dropdown.我想通过下拉的
onchange
事件将 position 动态更改到同一行的左侧。
I did this way:我是这样做的:
document.getElementById('divButtonGo').style.Paddingleft="80px"
How do I do this with Javascript?如何使用 Javascript 执行此操作?
The example you have provided already is javascript.您已经提供的示例是javascript。 If you want to change what triggers the code to run, change where you place it.
如果要更改触发代码运行的内容,请更改放置它的位置。
from an onchange
event, into a function
in a script tag that is called by something else.从
onchange
事件,进入由其他东西调用的脚本标记中的function
。
example例子
<input type="button" onclick="movediv()" />
<script>
function movediv(){
document.getElementById('divButtonGo').style.Paddingleft="80px"
}
</script>
There's several things wrong here.这里有几个问题。 In order of increasing importance:
按照重要性增加的顺序:
i
tag.i
标签中的右斜杠。elem.style.paddingLeft
, not elem.style.Paddingleft
elem.style.paddingLeft
,而不是elem.style.Paddingleft
<script>
tags.<script>
标记中。 All Javascript has to be wrapped in these tags, and , in order for that code to operate sucessfully, it's going to have to be placed after the "divButtonGo", or you'll have to wire up an onload event, like window.onload = function() { /* Bombs away; */ };
window.onload = function() { /* Bombs away; */ };
window.onload = function() { /* Bombs away; */ };
Your final result should look something like...您的最终结果应该类似于...
<div id="divButtonGo">
My Awesome Content
</div>
<script type="text/javascript">
var el = document.getElementById("divButtonGo");
el.style.paddingLeft = "30px";
</script>
Also, to note, padding wont' exactly change the position of the div
, only the position of the content inside.另外,需要注意的是,填充不会完全改变
div
的 position ,只会改变里面内容的 position 。 If you want to change the position of the div
, use margin-left
(in JS, element.style.marginLeft
, i believe)如果要更改
div
的 position ,请使用margin-left
(在 JS 中, element.style.marginLeft
,我相信)
I forgot you wanted it in the onchange event of a dropdown;我忘了你想要它在下拉的 onchange 事件中; so you'd do somethign like:
所以你会做类似的事情:
var dropdown = document.getElementById("MyDropDown");
dropdown.onchange = function() {
var el = document.getElementById("divButtonGo");
el.style.paddingLeft = "30px";
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.