[英]display specific div or content based on URL
我希望能夠顯示包含一些特定於 URL 的內容的 div。 所以只在包含指定 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);
您不需要為此使用正則表達式。 將本地路徑放入一個數組中,然后測試當前位置是否與這些路徑中的任何一個匹配:
var paths = ['/en-us/mens/designers/brunello_cucinelli', '/en-us/mens/grooming', '/en-us/mens/shoes'];
$('.container-content').toggle(paths.indexOf(location.path) != -1);
這不再需要使用 jQuery 作為 Liquid 對象請求檢索當前路徑: 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 %}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.