简体   繁体   English

使用 javascript 动态更改 div 标签的 position

[英]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:按照重要性增加的顺序:

  1. You're missing the closing slash from your i tag.您缺少i标签中的右斜杠。
  2. I don't see a "divButtonGo" in your html.我在您的 html 中没有看到“divButtonGo”。 If it's not there at all, obviously it won't work.如果它根本不存在,显然它不会起作用。 If it is, include it in your code snippet.如果是,请将其包含在您的代码片段中。
  3. I'm pretty sure to set the style you're going to need elem.style.paddingLeft , not elem.style.Paddingleft我很确定设置您需要的样式elem.style.paddingLeft ,而不是elem.style.Paddingleft
  4. Your script isn't wrapped inside <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; */ };所有 Javascript 都必须包含在这些标签中,并且,为了使该代码成功运行,它必须放在“divButtonGo”之后否则您必须连接一个 onload 事件,例如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 ,我相信)

EDIT:编辑:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM