将 div 链接到 HTML 中不同页面上的特定部分
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7314990/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Link a div to a particular section on a different page in HTML
提问by Geetanjali
hey I have a new dilemma.....
嘿我有一个新的困境.....
I have 3 div like 3 boxes and in each div there is an image and some text written and i want that when i click anywhere in any if the box then it will go to the other page e.g. if i am on home page then on clicking on box then it will go to service.html page in the website.
我有 3 个 div 像 3 个盒子,在每个 div 中都有一个图像和一些文字,我希望当我点击任何地方的任何地方时,如果盒子然后它会转到另一个页面,例如,如果我在主页上然后点击在框中,然后它将转到网站中的 service.html 页面。
I have done this by
我已经这样做了
<div onClick="document.location='service.html';">
and its working.but i want that when user click on first divthen it go to service .html and in that particular sectionand on second box it will go to another section on service.html and so on.......
和它的工作。但我希望当用户点击第一个 div然后它去服务 .html 和在那个特定的部分和第二个框它会去另一个部分 service.html 等等......
To be more clear,I want to link a div to a particular section to the particular page..
更清楚地说,我想将一个 div 链接到特定页面的特定部分。
How can I do this??
我怎样才能做到这一点??
采纳答案by Joseph Silber
You'll have to add an ID to the elements on the service.html
page you want to link to, and then include them in your links:
您必须为service.html
要链接到的页面上的元素添加一个 ID ,然后将它们包含在您的链接中:
<div onClick="window.location.href='service.html#section-1';">Go to Section 1</div>
<div onClick="window.location.href='service.html#section-2';">Go to Section 2</div>
P.S. Any reason you're using Javascript instead of regular anchor tags?
PS 您使用 Javascript 而不是常规锚标记的任何原因?
回答by Geetanjali
hey i have get ma answer......
嘿,我得到了答案......
<div onClick="document.location='service.html#section1_ID';">
<div onClick="document.location='service.html#section2_ID';">
<div onClick="document.location='service.html#section3_ID';">
and on that section I have used that particular
在那个部分我使用了那个特殊的
<div id="section1_ID">bla bla bla</div>
and so on.............
等等.............
回答by banditpanda
Look on below.
往下看。
// jQuery method
$('.location').bind("click", function() {
window.location = $(this).data('location');
});
<div class="collection-item location"
data-location="service.html#section1_ID" >
Lorem ipsum, blah
</div>