onclick提交按钮锚到div

时间:2013-11-28 10:42:31

标签: javascript php jquery html anchor

我有1个输入类型=“提交”,当我点击它时,我想打开一个div并自己锚定它。

我试过这个,但不行。

<form method='post'>
   <input type='submit' onclick='anchor_it();' value='Segnala' name='segnala' style='border:0; background: transparent; margin: 0; cursor: pointer;'>
</form>
<script>
function anchor_it(){
    document.location.href = '#segnala';
}
</script>
<?
if($_SERVER['REQUEST_METHOD'] === "POST"){
   echo "<div name='segnala'>xxx</div>";
}
?>

如果我点击,我的网址变成了www.mysite.com/page.php#segnala,但没有把我固定到div名称segnala

------ EDIT 我已经解决了。

我已经解决了。

<a href='#segnala' onclick='redirect();' style='border:0; background: transparent; margin: 0;'>Segnala</a>
<script>
function redirect(){
    $("#segnala").show();
}
</script>
<div align="center" id="segnala" style="display: none;">
<br>
    prova
</div>

1 个答案:

答案 0 :(得分:0)

你可以尝试一下:

<form method='post' action='?archor_it'>
   <input type='submit' onclick='anchor_it();return false;' value='Segnala' name='segnala' style='border:0; background: transparent; margin: 0; cursor: pointer;'>
</form>

<script>
function anchor_it(){
    document.location.href = '#segnala';
}
<?php if(isset($_GET['archor_it'])): ?>
anchor_it();
<?php unset($_GET['archor_it']); endif; ?>
</script>

<?
if($_SERVER['REQUEST_METHOD'] === "POST"){
   echo "<div name='segnala'>xxx</div>";
}
?>