从网址获取css ID [英] Get css id from url
本文介绍了从网址获取css ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这可能是一个愚蠢的问题,但有一种方法来获取css id从网址中选择?
例如在像这样的网址中:
www.website.com#adiv
使用Javascript / jQuery获取 #adiv
$ b
编辑
我的脚本到目前为止:
$(document).ready(function(){
var c = window.location.hash;
$(c).addClass ;
$('a')。click(function(){
$('。current')。removeClass('current');
$(this).addClass ');
});
});
Html:
<!doctype html>
< html lang =en>
< head>
< meta charset =utf-8>
< title>标题< / title>
< link rel =stylesheethref =style.css>
< script src =jquery-1.4.2.min.js>< / script>
< script type =text / javascript>
$(document).ready(function(){
var c = window.location.hash;
$(c).addClass('current');
$ 'a')。click(function(){
$('。current')。removeClass('current');
$(this).addClass('current');
};
$('a [href * =#]')click(function(){
if(location.pathname.replace(/ ^ \ //,'')== this.pathname.replace(/ ^ \ //,'')
&&& location.hostname == this.hostname){
var $ target = $(this.hash);
$ target = $ target.length& $ target || $('[name ='+ this.hash.slice(1)+']');
if($ target.length ){
var targetOffset = $ target.offset()。top;
$('html,body')。animate({scrollTop:targetOffset},1000);
return false;
}
}
});
});
< / script>
< / head>
< body>
< nav>
< a href =#header>第一< / a>
< a href =#second>第二< / a>
< / nav>
< section id =header>
...
< / section>
< section id =history>
...
< / section>
< / body>
< / html>
解决方案
var hash = window.location.hash;
$(hash).addClass(...);
或直接
code> $(window.location.hash).addClass(...);
It's probably a silly question but is there a way to get the css id selected from the url? For example in a url like this:
www.website.com#adiv
to get #adiv
using Javascript/jQuery?
Edit
My script so far:
$(document).ready(function() {
var c = window.location.hash;
$(c).addClass('current');
$('a').click(function (){
$('.current').removeClass('current');
$(this).addClass('current');
});
});
Html:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Title</title>
<link rel="stylesheet" href="style.css">
<script src="jquery-1.4.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
var c = window.location.hash;
$(c).addClass('current');
$('a').click(function () {
$('.current').removeClass('current');
$(this).addClass('current');
});
$('a[href*=#]').click(function() {
if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')
&& location.hostname == this.hostname) {
var $target = $(this.hash);
$target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');
if ($target.length) {
var targetOffset = $target.offset().top;
$('html,body').animate({scrollTop: targetOffset}, 1000);
return false;
}
}
});
});
</script>
</head>
<body>
<nav>
<a href="#header">First</a>
<a href="#second">Second</a>
</nav>
<section id="header">
...
</section>
<section id="history">
...
</section>
</body>
</html>
解决方案
That would be window.location.hash
var hash = window.location.hash;
$(hash).addClass(...);
Or directly
$(window.location.hash).addClass(...);
这篇关于从网址获取css ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文