我希望能够显示一个div包含一些内容是URL特定的。因此只在包含指定URL的页面上显示div,例如:
如果URL等于以下任意一项:
/makeup/face/foundation/compact-loose-powder/luminous-silk-compact-case/3614270283604.html
/makeup/face/foundation/liquid-foundation/designer-lift-foundation/3605521491268.html
/makeup/face/foundation/liquid-foundation/power-fabric-foundation/ww-00049-arm.html
/makeup/face/foundation/liquid-foundation/maestro-fusion-makeup/AP10123.html
/makeup/face/foundation/face-fabric/ww-00115-arm.html显示以下div:
<div class="container-content">content</div>否则就把它藏起来。
我已经尝试了以下几种方法,但都没有成功:
var paths = ['/makeup/face/foundation/compact-loose-powder/luminous-silk-
compact-case/3614270283604.html', '/makeup/face/foundation/compact-loose-
powder/luminous-silk-compact-case/3614270283604.html', '/makeup/face/foundation/liquid-foundation/power-fabric-foundation/ww-00049-arm.html', '/makeup/face/foundation/liquid-foundation/maestro-fusion-makeup/AP10123.html', '/makeup/face/foundation/face-fabric/ww-00115-arm.html'];
$('.container-content').toggle(paths.indexOf(location.path) != -1);发布于 2019-07-03 18:12:25
为此,您不需要正则表达式。将本地路径放入数组中,然后测试当前位置是否与这些路径中的任何一个匹配:
var paths = ['/en-us/mens/designers/brunello_cucinelli', '/en-us/mens/grooming', '/en-us/mens/shoes'];
$('.container-content').toggle(paths.indexOf(location.path) != -1);发布于 2019-07-04 18:25:52
这不再需要使用jQuery作为液体对象请求检索当前路径:https://help.shopify.com/en/themes/liquid/objects/request
所以这应该能起到作用:
{%- capture urls -%}
/makeup/face/foundation/compact-loose-powder/luminous-silk-compact-case/3614270283604.html, /makeup/face/foundation/liquid-foundation/designer-lift-foundation/3605521491268.html, /makeup/face/foundation/liquid-foundation/power-fabric-foundation/ww-00049-arm.html, /makeup/face/foundation/liquid-foundation/maestro-fusion-makeup/AP10123.html, /makeup/face/foundation/face-fabric/ww-00115-arm.html
{%- endcapture - %}
{% if urls contains request.path %}
<div class="container-content">content</div>
{% endif %}https://stackoverflow.com/questions/56867705
复制相似问题