我有以下HTML:的jQuery的if..else基於哈希ID提取條件語句
<section>
<div class="main" id="second"> </div>
</section>
<section>
<div class="main" id="third"></div>
</section>
<section>
<div class="main" id="fourth"></div>
</section>
而且下面的jQuery代碼:
<script>
navlist = [];
$("#navlist a").each(function(i) {
var thisLink = $(this);
var thisId = thisLink.attr('href');
var thisTarget = $(thisId);
navlist.push({
'anchor': thisLink,
'id': thisId,
'target': thisTarget
});
thisLink.on('click', function(e) {
e.preventDefault();
$('html, body').animate({
scrollTop: thisTarget.offset().top
}, 800);
});
});
$(window).on('scroll resize', function(e) {
$.each(navlist, function(e, elem) {
var placement = elem.target[0].getBoundingClientRect();
if(placement.top<window.innerHeight && placement.bottom>0) {
history.pushState({}, '', elem.id);
console.log('Hash: ' + elem.id);
return false; /* Exit $.each loop */
};
});
});
</script>
腳本的工作是讓平滑滾動在站點上的不同錨點之間以及在點擊或滾動時更改URL #id。我想在這裏使用帶有從URL中獲取的id的條件語句。
例如:
if(grabbedhashid == "second"){
$(".phone").css('display', 'none');
}else{
//display
}
但我沒有任何想法,從那裏我可以做到這一點。請幫助我們。
示例URL與ID:http://localhost/sites/fh/index.php#first
你可以分享你的ID爲 –
代碼片段或騙取錢財的URL的一個會更好 –
@ÖzgürErsil更新...但它僅適用於想法和不在本地主機上查看.. –