简体   繁体   English

链接到同一页面上的特定选项卡

[英]Linking to a specific tab on the same page

On my product pages, I want to have aa link that will take the user to a specific tab below. 在我的产品页面上,我想要一个链接,该链接会将用户带到下面的特定标签。

The three tabs on each product page are: Description, Reviews, Order Form . 每个产品页面上的三个选项卡是: Description, Reviews, Order Form

I've created the link which is basic and appears as: 我创建了基本链接,显示为:

<a href="#tab-name">ORDER</a>.

The tab below is set up as follows: 下面的标签设置如下:

<div id="tab-name">tab info</div>

A simple link like the one above doesn't work. 像上面的链接这样的简单链接不起作用。

When someone opens a product page, the Description tab is the default open tab, which is fine. 有人打开产品页面时,“描述”选项卡是默认的打开选项卡,这很好。

How can I create a simple link that when clicked will take the user to the Order Form tab? 如何创建一个简单的链接,单击该链接即可将用户带到“订购表单”选项卡?

I've searched in so many places and most explanations show solutions when you link to a specific tab on one page from another page. 我搜索了很多地方,当您链接到另一页面上的特定选项卡时,大多数解释都显示了解决方案。

I'm using WordPress as my CMS. 我使用WordPress作为CMS。

Does something like this need Javascript to make it work? 这样的事情需要Javascript使其起作用吗? If so, what (simple) code could accomplish this? 如果是这样,那么什么(简单的)代码可以完成此任务? (I'm not a developer.) (我不是开发人员。)

Thank you. 谢谢。

What i understood is you have three div one after other in page and all are visible at same time. 我了解的是您在页面中一个接一个地接三个div,并且所有这些都是同时可见的。 Note: if at a time only one div is visible and other is hidden and on click of anchor you need to show it and than scroll to that position in that case you will need javascript 注意:如果一次只能看到一个div,而另一个则是隐藏的,则单击锚点时需要显示它,然后滚动到该位置,则需要javascript

I hope this is not your case in your case all the div are visible and on click of anchor you need to scroll to that particular div, in that case you don't need any javascript and your code should work. 我希望这不是您的情况,在所有div都可见的情况下,单击锚点您需要滚动到该特定div,在这种情况下,您不需要任何JavaScript,并且您的代码也可以工作。

Here is working demo i have made in jsfiddle 这是我在jsfiddle中制作的工作演示

http://jsfiddle.net/vickykumarui/n2wzksdg/ http://jsfiddle.net/vickykumarui/n2wzksdg/

sample HTML code is 示例HTML代码是

<a href = "#div1">Go to div 1</a>
<a href = "#div-2">Go to div 2</a>
<a href = "#div3">Go to div 3</a>
<a href = "#div4">Go to div 4</a>


<div id="div1">
<h4>  somed ummy content div1</h4>
<p> somed ummy content</p>
<p>   somed ummy content</p>
    <p>   somed ummy content </p>
  <p>   somed ummy content </p>
  <p>   somed ummy content </p>
  <p>   somed ummy content </p>
  <p>   somed ummy content </p>
  <p>   somed ummy content </p>
</div>
<div id="div-2">
<h4>  somed ummy content div-2</h4>
<p> somed ummy content</p>
<p>   somed ummy content</p>
    <p>   somed ummy content </p>
  <p>   somed ummy content </p>
  <p>   somed ummy content </p>
  <p>   somed ummy content </p>
  <p>   somed ummy content </p>
  <p>   somed ummy content </p>
</div>
<div id="div3">

<h4>  somed ummy content div3</h4>
<p> somed ummy content</p>
<p>   somed ummy content</p>
    <p>   somed ummy content </p>
  <p>   somed ummy content </p>
  <p>   somed ummy content </p>
  <p>   somed ummy content </p>
  <p>   somed ummy content </p>
  <p>   somed ummy content </p>
</div>


<div id="div4">

<h4>  somed ummy content div4</h4>
<p> somed ummy content</p>
<p>   somed ummy content</p>
    <p>   somed ummy content </p>
  <p>   somed ummy content </p>
  <p>   somed ummy content </p>
  <p>   somed ummy content </p>
  <p>   somed ummy content </p>
  <p>   somed ummy content </p>
</div>

Now when you run the above code in jsfiddle you will observe that on click of anchor page is scrolling to respective div but on click of fourth anchor page is not scrolling to div4 that is because div4 is at bottom of page and it does not have space to scroll below. 现在,当您在jsfiddle中运行上面的代码时,您会发现单击锚点页面时滚动到相应的div,但单击第四锚点页面时没有滚动到div4,这是因为div4在页面底部并且没有空间在下面滚动。 Hope it helps 希望能帮助到你

Updated above fiddle which initially hides div2, div3,div4 and on click of anchor tag it will show respective div and scroll to that position. 在上方的小提琴上进行了更新,该小提琴最初隐藏了div2,div3,div4,并在点击锚标记后会显示相应的div并滚动到该位置。 Now changed code is 现在更改的代码是

HTML CODE : Changes from previous code is 1. initially each function will have class display noe which is responsible for hidding div and added onclick function like this Go to div 2 this function in js is responsible for showing div onclick of respective html HTML代码:与先前代码的更改为1。最初,每个函数将具有class display noe,该类负责隐藏div,并添加了此类的onclick函数转到div 2 js中的此函数负责显示各个html的div onclick

<a href = "#div1">Go to div 1</a>
<a onclick = "showDiv('div-2')"
href = "#div-2">Go to div 2</a>
<a onclick = "showDiv('div3')" href = "#div3">Go to div 3</a>
<a onclick = "showDiv('div4')" href = "#div4">Go to div 4</a>


<div id="div1">
<h4>  somed ummy content div1</h4>
<p> somed ummy content</p>
<p>   somed ummy content</p>
    <p>   somed ummy content </p>
  <p>   somed ummy content </p>
  <p>   somed ummy content </p>
  <p>   somed ummy content </p>
  <p>   somed ummy content </p>
  <p>   somed ummy content </p>
</div>
<div id="div-2" class = "displayNone">
<h4>  somed ummy content div-2</h4>
<p> somed ummy content</p>
<p>   somed ummy content</p>
    <p>   somed ummy content </p>
  <p>   somed ummy content </p>
  <p>   somed ummy content </p>
  <p>   somed ummy content </p>
  <p>   somed ummy content </p>
  <p>   somed ummy content </p>
</div>
<div id="div3" class = "displayNone">

<h4>  somed ummy content div3</h4>
<p> somed ummy content</p>
<p>   somed ummy content</p>
    <p>   somed ummy content </p>
  <p>   somed ummy content </p>
  <p>   somed ummy content </p>
  <p>   somed ummy content </p>
  <p>   somed ummy content </p>
  <p>   somed ummy content </p>
</div>


<div id="div4" class = "displayNone">

<h4>  somed ummy content div4</h4>
<p> somed ummy content</p>
<p>   somed ummy content</p>
    <p>   somed ummy content </p>
  <p>   somed ummy content </p>
  <p>   somed ummy content </p>
  <p>   somed ummy content </p>
  <p>   somed ummy content </p>
  <p>   somed ummy content </p>
</div>

CSS CODE CSS代码

.displayNone{
  display:none
}

JS CODE JS代码

var showDiv = function(id){
var element = document.getElementById(id);
element.classList.remove('displayNone')
}

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

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