javascript - 如果指定的域不等于,则当前 URL 应用此 jQuery 以及具有相同域的页面

标签 javascript jquery if-statement url jquery-events

下面的代码仅显示<span>http://example.com/但不会显示<span>http://example.com/files/target.html那么如何让它在具有指定域的所有页面上工作呢?请帮忙。

<script type="text/javascript">
var myurl = "http://example.com/";
var currenturl = window.location
if(myurl != currenturl) {
$("<span style=font-size:200px;>big</span>").replaceAll("body"); // check replaceWith() examples
}
</script>

最佳答案

这应该有效:

<script type="text/javascript">
    var myurl = "www.myurl.com";
    var currenturl = window.location.hostname;
    if(myurl != currenturl) {
        $("<span style=font-size:200px;>big</span>").replaceAll("body"); // check replaceWith() examples
    }
</script>

根据 MDN 文档:https://developer.mozilla.org/en/window.location

关于javascript - 如果指定的域不等于,则当前 URL 应用此 jQuery 以及具有相同域的页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7629640/

相关文章:

javascript - Angular Directive(指令) $watch 没有响应

javascript - 获取字体向量

javascript - 当用户使用 jquery 不断单击按钮时需要 append html 元素

sql - 使用 if 语句如何替换值

python - 将 django 中的价格字段转换为特定货币

java - 如何使用 if 函数调用数组元素

javascript - 使用 d3.js 向饼图添加工具提示

javascript - 竞争条件 : Trying to play a video before it loads React

javascript - load() 触发多次

javascript - Windows 商店应用程序 - jQuery 错误