使用 Javascript 在每次点击时刷新锚标记
我有一个电子商务网站,每次有人点击“添加到托盘”(我们称之为托盘而不是购物车)时,页面都会刷新到同一页面并将其自身定位到该产品的 div(在这里尝试一下: http://www.boostliquidation.com/)。这是使用以下 Javascript 实现的:
$('input.returnLink').each(function(){
$(this).attr('value',window.location.href + $(this).attr('data-skuhash'));
});
这个锚标记放置在“添加到购物车”按钮之前的隐藏输入标记中:
<input type="hidden" value="back" name="return_to" class='returnLink' data-skuhash='#{% for variant in product.variants %}{{variant.sku}}{% endfor %}' />
({% %} 中的代码是 Shopify 网站使用的一种名为 Liquid 的语言)。
这工作得很好......但仅限于第一个产品。它会在地址栏中添加前面带有 # 的 SKU 编号。如果用户添加其他产品,它会刷新到页面顶部,因为它将 SKU 编号修改为地址栏中已显示的现有 SKU 编号。
如何在第一个产品之后添加产品,用自己的 SKU 替换现有的 SKU,以便它们刷新回产品而不是返回页面顶部?
I have an ecommerce site that, every time someone clicks on "Add to Pallet" (we call it a pallet instead of a cart) the page refreshes to the same page and positions itself to the div of that product (try it out here: http://www.boostliquidation.com/). This happens using this Javascript:
$('input.returnLink').each(function(){
$(this).attr('value',window.location.href + $(this).attr('data-skuhash'));
});
And this anchor tag which is placed in a hidden input tag before the "Add to Cart" button:
<input type="hidden" value="back" name="return_to" class='returnLink' data-skuhash='#{% for variant in product.variants %}{{variant.sku}}{% endfor %}' />
(The code in the {% %} is a language called Liquid used with Shopify websites).
This works fine... but only on the first product. It adds the SKU number preceded by a # in the address bar. If a user adds another product, it refreshes to the top of the page, because it amends the SKU number to the existing SKU number already showing in the address bar.
How can I get the products added after the first to replace the existing SKU with their own so they'll refresh back to the product and not back to the top of the page?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我可能会尝试使用 string.replace()
查找网址末尾的任何哈希值,如果找到则替换,如果没有则添加。
I'd probably try to use the
string.replace()
Looks for any hashes on the end of the url, replaces if it finds one, adds one if it doesn't.