打开一个点击按钮的网页

时间:2018-01-18 12:24:07

标签: html

我正在用HTML编写代码,它有2个按钮,比如ab。我想在我的URL中传递一些会触发按钮的内容。我知道我们是如何为div做的。

<div class="a">
  <a name="a" /> This is Div A
</div>
<div class="b">
  <a name="b" /> This is Div B
</div>

我可以直接通过myURL.com#a / myURL.com/#b,转到那个div。但我想知道我是否可以使用按钮来完成它。

由于

3 个答案:

答案 0 :(得分:1)

是的,您可以使用以下某些JavaScript执行此操作:

<button onclick="window.location='#a';">Go to Div A</button>

答案 1 :(得分:0)

如果您希望任何一个按钮A OR 按钮B可见,您可以使用此功能:

<强> CSS

.button {
    display: none;
    }
.button:target {
    display: block
    }

<强> HTML

<!-- link in the same page -->
<a href="#A">go to button 1</a><br>
<a href="#B">go to button 2</a><br>

<!-- link from external page -->
<a href="your_url.com/#A">go to button A</a><br>
<a href="your_url.com/#B">go to button B</a><br>

<!-- buttons will only be visable when linked to -->
<a class="button" id="A"><input type="button" value="button A"></a>
<a class="button" id="B"><input type="button" value="button B"></a>

如果你只想跳到按钮,你回答了自己的问题。 只需将<div id="A">包裹在按钮周围。

答案 2 :(得分:-1)

首先将id属性添加到div。例如; <div class="b" id="part-b"> 然后只需链接到mypage.html#part-b,例如; <a href="mypage.html#part-b>Go to part b</a>

相关问题