admin管理员组文章数量:1023204
I am using this snippet to fade content into a div when a specific link is clicked....
<script language="JavaScript">
$(document).ready(function () {
$('#section1, #section2, #section3').addClass('js');
$('#content-container a').click(function (e) {
e.preventDefault();
var rel = $(this).attr('rel');
$('#' + rel).fadeIn().siblings('div').fadeOut();
});
var link = document.URL.split('#')[1];
if (link) {
var el = $('#' + link);
if (el) el.click();
}
});
</script>
Is there a way to load the specified content using a URL instead of clicking? So I can link to www.mydomain/mypage.php#section2 or something similar?
UPDATE
Here is a jsfiddle of the simplified code /
I am using this snippet to fade content into a div when a specific link is clicked....
<script language="JavaScript">
$(document).ready(function () {
$('#section1, #section2, #section3').addClass('js');
$('#content-container a').click(function (e) {
e.preventDefault();
var rel = $(this).attr('rel');
$('#' + rel).fadeIn().siblings('div').fadeOut();
});
var link = document.URL.split('#')[1];
if (link) {
var el = $('#' + link);
if (el) el.click();
}
});
</script>
Is there a way to load the specified content using a URL instead of clicking? So I can link to www.mydomain./mypage.php#section2 or something similar?
UPDATE
Here is a jsfiddle of the simplified code http://jsfiddle/k6RhR/
Share Improve this question edited Mar 7, 2014 at 15:43 fightstarr20 asked Mar 6, 2014 at 12:10 fightstarr20fightstarr20 12.7k45 gold badges171 silver badges301 bronze badges 4-
What will be the values of
link
? – Satpal Commented Mar 6, 2014 at 12:14 -
You mean when people open the URL with the # already attached? It looks like you're already doing that by doing
el.click()
. I.e. Faking a user click. – Gerben Jacobs Commented Mar 6, 2014 at 12:14 -
Are you looking for this
onhashchange
? – Rahil Wazir Commented Mar 6, 2014 at 12:16 -
Have you tried
window.location.href = link
? – Pavlo Commented Mar 6, 2014 at 12:29
3 Answers
Reset to default 3You can do something like this:
if(window.location.hash && window.location.hash=="#section2") {
// Do stuff that are meant to happen when this hash is present.
}
try This
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>mouseover demo</title>
<script src="http://code.jquery./jquery-2.1.0.min.js"></script>
<style>
.invisible
{
display: none;
}
.lorem
{
height: 300px;
}
</style>
<script type="text/javascript">
$(document).ready(function () {
getContent();
});
$(window).bind('hashchange', function (e) {
getContent();
});
function getContent() {
var url = window.location.toString();
var hash = url.substring(url.indexOf('#'));
$('html, body').animate({
scrollTop: $(hash).offset().top
}, 2000);
$(hash).fadeIn();
$(hash).siblings("div").fadeOut();
}
</script>
</head>
<body>
<div class="lorem">
<div id="section1" class="invisible">
Content 1
<p style="text-align: justify;">
Content 1 content</p>
</div>
<div id="section2" class="invisible">
Content 2
<p style="text-align: justify;">
Content 2 content</p>
</div>
<div id="section3" class="invisible">
Content 3
<p style="text-align: justify;">
Content 3 content</p>
</div>
<div id="section4" class="invisible">
Content 4
<p style="text-align: justify;">
Content 4 content
</p>
</div>
</div>
<div id="links">
<a href="#section1" rel="section1" id="section1">Section 1</a>
<br />
<a href="#section2" rel="section2" id="section2">Section 2</a>
<br />
<a href="#section3" rel="section3" id="section3">Section 3</a>
<br />
<a href="#section4" rel="section4" id="section3">Section 4</a>
</div>
</body>
</html>
Is this what you are looking for. On load the page will look for the #section to show the content. On click of the anchor the hash link will be changed and the hash change function will load in the required content.
If you have any query please let me know.
jQuery load() allows you to specify the portion of the page like:
$( "#container" ).load( "/mypage.php #section2" );
I am using this snippet to fade content into a div when a specific link is clicked....
<script language="JavaScript">
$(document).ready(function () {
$('#section1, #section2, #section3').addClass('js');
$('#content-container a').click(function (e) {
e.preventDefault();
var rel = $(this).attr('rel');
$('#' + rel).fadeIn().siblings('div').fadeOut();
});
var link = document.URL.split('#')[1];
if (link) {
var el = $('#' + link);
if (el) el.click();
}
});
</script>
Is there a way to load the specified content using a URL instead of clicking? So I can link to www.mydomain/mypage.php#section2 or something similar?
UPDATE
Here is a jsfiddle of the simplified code /
I am using this snippet to fade content into a div when a specific link is clicked....
<script language="JavaScript">
$(document).ready(function () {
$('#section1, #section2, #section3').addClass('js');
$('#content-container a').click(function (e) {
e.preventDefault();
var rel = $(this).attr('rel');
$('#' + rel).fadeIn().siblings('div').fadeOut();
});
var link = document.URL.split('#')[1];
if (link) {
var el = $('#' + link);
if (el) el.click();
}
});
</script>
Is there a way to load the specified content using a URL instead of clicking? So I can link to www.mydomain./mypage.php#section2 or something similar?
UPDATE
Here is a jsfiddle of the simplified code http://jsfiddle/k6RhR/
Share Improve this question edited Mar 7, 2014 at 15:43 fightstarr20 asked Mar 6, 2014 at 12:10 fightstarr20fightstarr20 12.7k45 gold badges171 silver badges301 bronze badges 4-
What will be the values of
link
? – Satpal Commented Mar 6, 2014 at 12:14 -
You mean when people open the URL with the # already attached? It looks like you're already doing that by doing
el.click()
. I.e. Faking a user click. – Gerben Jacobs Commented Mar 6, 2014 at 12:14 -
Are you looking for this
onhashchange
? – Rahil Wazir Commented Mar 6, 2014 at 12:16 -
Have you tried
window.location.href = link
? – Pavlo Commented Mar 6, 2014 at 12:29
3 Answers
Reset to default 3You can do something like this:
if(window.location.hash && window.location.hash=="#section2") {
// Do stuff that are meant to happen when this hash is present.
}
try This
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>mouseover demo</title>
<script src="http://code.jquery./jquery-2.1.0.min.js"></script>
<style>
.invisible
{
display: none;
}
.lorem
{
height: 300px;
}
</style>
<script type="text/javascript">
$(document).ready(function () {
getContent();
});
$(window).bind('hashchange', function (e) {
getContent();
});
function getContent() {
var url = window.location.toString();
var hash = url.substring(url.indexOf('#'));
$('html, body').animate({
scrollTop: $(hash).offset().top
}, 2000);
$(hash).fadeIn();
$(hash).siblings("div").fadeOut();
}
</script>
</head>
<body>
<div class="lorem">
<div id="section1" class="invisible">
Content 1
<p style="text-align: justify;">
Content 1 content</p>
</div>
<div id="section2" class="invisible">
Content 2
<p style="text-align: justify;">
Content 2 content</p>
</div>
<div id="section3" class="invisible">
Content 3
<p style="text-align: justify;">
Content 3 content</p>
</div>
<div id="section4" class="invisible">
Content 4
<p style="text-align: justify;">
Content 4 content
</p>
</div>
</div>
<div id="links">
<a href="#section1" rel="section1" id="section1">Section 1</a>
<br />
<a href="#section2" rel="section2" id="section2">Section 2</a>
<br />
<a href="#section3" rel="section3" id="section3">Section 3</a>
<br />
<a href="#section4" rel="section4" id="section3">Section 4</a>
</div>
</body>
</html>
Is this what you are looking for. On load the page will look for the #section to show the content. On click of the anchor the hash link will be changed and the hash change function will load in the required content.
If you have any query please let me know.
jQuery load() allows you to specify the portion of the page like:
$( "#container" ).load( "/mypage.php #section2" );
本文标签: jqueryTrigger Javascript with URL parameterStack Overflow
版权声明:本文标题:jquery - Trigger Javascript with URL parameter - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745542544a2155248.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论