如果URL包含单词,则隐藏div [英] Hide div if URL contains word

查看:89
本文介绍了如果URL包含单词,则隐藏div的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果页面的网址包含某个字词,我需要隐藏div。感谢这个网站,我已经能够成功找到该网址是否包含该字词。此代码有效:

I need to hide a div if the url of the page contains a certain word. Thanks to this site I have been able to successfully find if the url contains the word. This code works:

<script type="text/javascript">
if (window.location.href.indexOf("Bar-Ends") != -1) {
alert("your url contains bar ends");
}
</script>

但由于某种原因,它无法隐藏div,如下所示:

but for some reason it will not work to hide a div, like this:

<script type="text/javascript">
if (window.location.href.indexOf("Bar-Ends") != -1) {
$("#notBarEnds").hide();
}
</script>

<div id="notBarEnds">this is not bar ends</div>

任何人都知道这段代码有什么问题?
非常感谢任何帮助
谢谢

Anyone have any idea what is wrong with this code? Any help is greatly appreciated Thanks

推荐答案

请注意重新订购:

<div id="notBarEnds">this is not bar ends</div>

<script type="text/javascript">
if (window.location.href.indexOf("Bar-Ends") != -1) {
$("#notBarEnds").hide();
}
</script>

<script type="text/javascript">
$(document).ready(function () {
    if (window.location.href.indexOf("Bar-Ends") != -1) {
        $("#notBarEnds").hide();
    }
}
</script>

等待整个文件准备好

这篇关于如果URL包含单词,则隐藏div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆