如果电子邮件中的字词出现在网址div id =' sociallocker'应该显示和div id =' emaillocker'应该隐藏。
如果电子邮件不在网址中:div id =' sociallocker'应该隐藏和div id =' emaillocker'应该显示。
所以: URL包含电子邮件: 显示div:#sociallocker 隐藏div:#emaillocker
网址不包含电子邮件: 显示div:#emaillocker 隐藏div:#sociallocker
直播链接:https://www.moneynest.co.uk/test-page-for-stack/
使用当前代码,div都显示无论什么?
HTML
<div id="sociallocker">
[sociallocker id="1505"]
</div>
<div id="emaillocker">
[emaillocker]
[/emaillocker]
</div>
JS
<script>
if(document.location.href.indexOf("email") >= 0) {
$("#sociallocker").css(‘display’, ‘none’);
}
</script>
答案 0 :(得分:0)
试试这个。使用jQuery的show()
和hide()
。
//str="https://stackoverflow.com/questions/49467845/show-hide-div-dependant-on-url-variable/email";
if(document.location.href.indexOf("email") >= 0) { //insted of 'str' use document.location.href
$("#sociallocker").show();
$("#emaillocker").hide();
}
else{
$("#sociallocker").hide();
$("#emaillocker").show();
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="sociallocker">
[sociallocker id="1505"]
</div>
<div id="emaillocker">
[emaillocker]
[/emaillocker]
</div>
答案 1 :(得分:0)
根据我的最佳理解,我试图用假URL字符串模拟你的问题给你一个答案。
你有两种哈希参数的可能性:sociallocker或emaillocker,它将以#sociallocker或#emaillocker的方式显示在URL中。
正如您尝试在核心javascript中执行代码一样,我也在核心javascript中给出了答案。
这里我假设您有以下网址方案。
<强> https://www.moneynest.co.uk/test-page-for-stack/ 强>
// When you do in programming uncomment following line.
//var _href = document.location.href;
// When you do in programming comment following line.
var _href= "https://www.moneynest.co.uk/test-page-for-stack/#sociallocker";
var params = _href.split("#")[1];
var divEmailLocker = document.getElementById("emaillocker");
var divSocialLocker = document.getElementById("sociallocker");
if (params.indexOf("sociallocker") > -1) {
divSocialLocker.style.display = "block";
divSocialLocker.style.visibility = "visible";
divEmailLocker.style.display = "none";
divEmailLocker.style.visibility = "hidden";
}else if (params.indexOf("emaillocker") > -1) {
divSocialLocker.style.display = "none";
divSocialLocker.style.visibility = "hidden";
divEmailLocker.style.display = "block";
divEmailLocker.style.visibility = "visible";
}
&#13;
div#sociallocker, div#emaillocker{
display:none;
}
&#13;
<div id="sociallocker">
[sociallocker id="1505"]
</div>
<div id="emaillocker">
[emaillocker] [/emaillocker]
</div>
&#13;
答案 2 :(得分:-1)
试
document.location.href.includes("email")
检查单词&#34; email&#34;在网址中出现