2017-02-21 14 views
3

私は別のページのアンカーへのhrefリンクを作成しようとしていますが(2ページ目はそれをより簡単にする)、問題のアンカーはdiv内に含まれていますあなたが最初にそれを訪問したときに元々は隠されていたものです(ヘッダーがクリックされたときにdivが展開されて内容が表示されますので、デフォルトでdivには 'display:none'というプロパティがあります)。隠しdiv内の要素へのアンカー

私が話している事柄の正確な種類の小さなサンプルです。

<html> 
 

 
<head> 
 

 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> 
 
<style> 
 
p {color:#000;} 
 
</style> 
 

 
<script> 
 
$(document).ready(function(){ 
 
    $("#f-hdr").click(function(){ 
 
     $("#hidden").toggle(1000); 
 
    }); 
 
}); 
 
</script> 
 

 
</head> 
 

 

 

 

 
<body> 
 

 
<a href="#anch">Link to anchor within hidden div</a> 
 

 
<h2><a id="f-hdr" href="javascript:void(0)">Div Header</a></h2> 
 

 
<div id="hidden" style="display:none"> 
 

 
<p id="anch">some text</p> 
 

 
</div> 
 

 
</body> 
 
</html>

ありがとうございます!

EDIT - たとえば、「ページ1」にあった場合、「ページ2」(この場合は#anch)の特定の要素へのリンクを開くことができますが、「#anch」ページが最初に開かれたときに隠されているdiv(#hidden)に含まれています。もともと、 '#hidden'のコンテンツにアクセスするには、見出しをクリックする必要があります。これにより、divを展開してコンテンツを表示します。最初の見出しをクリックせずに '#anch'へのリンクをクリックして(「ページ2」に移動)、「#anch」のコンテンツを表示したいと考えています。

私が見たことから、それは 'onhashchange'イベントと関係があるかもしれません。私はあまり確かではありません(したがって、投稿):

+1

お困りですか? – bowl0stu

+0

明快さの欠如に対する謝罪、編集を参照してください。 –

答えて

0

W3Schoolsのにより示唆されるように名前のアンカー属性を使用してこれを達成するための二つの方法があります。

W3Schoolsの:https://www.w3schools.com/tags/att_a_name.asp

、別のアプローチは、名前のアンカーのように動作し、一般的なjqueryのメソッドを使用することができます。

HTML:

<a class="nameLink" href="#hidden" >Link to anchor within hidden div</a> 

<h2><a id="f-hdr" href="javascript:void(0)">Div Header</a></h2> 
<p class="dummy-height"></p> 
<div id="hidden" > 
    <p id="anch">some text</p> 
</div> 

はJQuery:

$(document).ready(function(){ 
    $("#f-hdr").click(function(){ 
     $("#hidden").toggle(1000); 
    }); 
    $(".nameLink").on('click', function(){ 
       $($(this).attr('href')).show(); 
    }) 
}); 

注:ここではnameLinkは同じfunctionlityを使用するための任意のアンカーリンクに割り当てることができるクラスです。

最後にリンク:

https://jsfiddle.net/Ashokkumargupta/bc1drx0s/

ホープこれはあなたの問題を解決します。

ありがとう、 アショク

+0

もっと暖かいですが、残念ながらそうではありません(申し訳ありませんが、質問の中で私の言葉の選択を後悔し始めました)私は何かに私が取り組んでいるものにもう少し近づくようにフィドルを修正しました - https://jsfiddle.net/bc1drx0s/5 /リンクをクリックして '私が探している人物'にリンクしたいと思っています。私は、これが不可能であることを理解しています。手動でdivを最初に展開せずに(私が探している人に)ジャンプするリンクが必要です(もし意味があれば) - ありがとうございました。 –

+0

私はコードとリンクを更新しました。チェックしてください。お役に立てれば! :) リンク:https://jsfiddle.net/Ashokkumargupta/bc1drx0s/ –

0

display: noneの代わりにvisibility: hiddenを使用する必要があります。なし手段:あなたはWhat is the difference between visibility:hidden and display:none?

表示この質問を確認することができます詳細については

<div id="hidden" style="visibility: hidden"> 
    <p id="anch">some text</p> 
</div> 

:これにより

<div id="hidden" style="display:none"> 
    <p id="anch">some text</p> 
</div> 

は、私はあなたがこれを変更する必要があるわけ問題のタグは、ページ上のすべてのの梨(あなたはまだそれをdomでやりとりすることができます)。 他のタグ間にスペースが割り当てられません。

可視性:なし、タグは、表示されていないが、スペースがページ上に割り当てられている:隠されたディスプレイとは違っていることを意味します。タグは です。ページには表示されません。

私は長いテキストは、あなたが「隠されたdiv要素の中に固定するためのリンク」をクリックしたとき、それは隠さ要素に行く方法を確認するためにそれに追加して更新を掲示しています:

$(document).ready(function(){ 
 
    $("#f-hdr").click(function(){ 
 
     $("#hidden").toggle(1000); 
 
    }); 
 
});
p {color:#000;}
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> 
 

 
<a href="#anch">Link to anchor within hidden div</a> 
 

 
<h2><a id="f-hdr" href="javascript:void(0)">Div Header</a></h2> 
 

 
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p> 
 
<p>It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).</p> 
 
<p>Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.</p> 
 
<p>There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don't look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isn't anything embarrassing hidden in the middle of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc.</p> 
 
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p> 
 
<p>It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).</p> 
 
<p>Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.</p> 
 
<p>There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don't look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isn't anything embarrassing hidden in the middle of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc.</p> 
 
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p> 
 
<p>It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).</p> 
 
<p>Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.</p> 
 
<p>There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don't look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isn't anything embarrassing hidden in the middle of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc.</p> 
 
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p> 
 
<p>It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).</p> 
 
<p>Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.</p> 
 
<p>There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don't look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isn't anything embarrassing hidden in the middle of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc.</p> 
 
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p> 
 
<p>It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).</p> 
 
<p>Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.</p> 
 
<p>There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don't look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isn't anything embarrassing hidden in the middle of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc.</p> 
 

 
<div id="hidden" style="visibility: hidden"> 
 
\t <p id="anch">some text</p> 
 
</div>

関連する問題