代码之家  ›  专栏  ›  技术社区  ›  Rey Young

javascript:window.location自动更改包含的链接

  •  0
  • Rey Young  · 技术社区  · 8 年前

    我使用了window.location(.assign,.replace,.ref)在单击时重定向到页面产品。但由于某些原因,它会自动更改某些Href链接。

    例如: 以前的href=“commercial/fonts/fonts.min.css”现在是href=“product/commercial/fonts/fonts.min.css”。

    上一个HTML文件

    <link rel="stylesheet" type="text/css" href="commercial/fonts/fonts.min.css" />
    

    在此单击事件触发器之后

    $('.productImage').on('click', function(){
            var product_id = $(this).data('id');
            window.location.assign("/product/"+product_id);
    });
    

    新建HTML

    <link rel="stylesheet" type="text/css" href="product/commercial/fonts/fonts.min.css" />
    

    产品将自动发送到Href。其他一些文件也会出现这种情况。如果是img src

    1 回复  |  直到 8 年前
        1
  •  0
  •   Matthias Schmidt    8 年前

    使用 / 在您的Href开头,如下所示: href="commercial/fonts/fonts.min.css" -这会使url相对于文档根目录。目前,您使用的是相对于当前文档的url。您可能需要阅读绝对URL和相对URL,例如 this answer 是的。