简体   繁体   English

Style.Top属性不会持续

[英]Style.Top property doesn't last

I have a problem with a php page. 我的php页面有问题。 What I want is a button that can put a table (inserted with php) to change its TOP from 310px to 500px. 我想要的是一个可以放置表格(插入php)的按钮,以将其TOP从310px更改为500px。 The table starts like this: 该表开始如下:

<?php
print('<table id="bloque" style="top:310px;">');
print("<tr>");
print("<th>Código</th>");
?>

My script is like this: 我的脚本是这样的:

<script type="text/javascript">
    function nuevo(){
        document.getElementById("bloque").style.top="500px";
    }       
</script>

And my button is like this: 我的按钮是这样的:

<INPUT TYPE="submit" VALUE="Nuevo Registro" onClick="nuevo();">

When I click on the button, the table goes to Top 500px, but immediately goes back to 310px. 当我单击该按钮时,表格会移至前500像素,但立即会回到310像素。 Is there a property that I am missing? 我有缺少的财产吗?

You are using on submit and not actual button. 您使用的是“提交”按钮,而不是实际按钮。 Change your HTML to 将您的HTML更改为

<button type="button" onclick="nuevo();">Nuevo Registro</button>

 function nuevo() { document.getElementById("bloque").style.top = "500px"; } 
 #bloque { position: absolute; } 
 <table id="bloque" style="top:310px;"> <tr> <th>Código</th> </tr> </table> <button onclick="nuevo();" type="button">Nuevo Registro</button> 

I think your code is correct, what happens is that you are using an input type submit in your code, so what happens is a page refresh. 我认为您的代码是正确的,发生的事情是您在代码中使用了输入类型Submit,因此发生的是页面刷新。 Then your html returns to the first phase. 然后,您的html返回到第一阶段。 One solution don't use a submit type button. 一种解决方案不使用提交类型按钮。

Example use: 使用示例:

<button id ="button" type="button" onclick="nuevo();">Nuevo Registro</button>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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