我想在網頁內容中重用部分 url
例如 domain.com/specialist/brand/location/
我想在網頁內容中多次重用“品牌”,并在網頁內容中多次重用“位置”。
首先我嘗試使用變數 url/?specialist=brand&bezorgen=city
但這不適用于以下方法,因為它只渲染一次
我試過了
<script>
const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);
const specialist = urlParams.get('specialist');
const bezorgen = urlParams.get('bezorgen');
document.getElementById("specialist").innerHtml = specialist;
document.getElementById("bezorgen").innerHtml = bezorgen;
</script>
and in the html
<span id=specialist></span>
<span id=bezorgen></span>
什么是讓這個作業的最快方法
技術 = 樹枝和 js
提前致謝
uj5u.com熱心網友回復:
枝條
當您使用自定義框架時,我建議您app.request.get模仿Symfony.
首先創建一個提供相同邏輯的類
請求.php
class Request {
public function __construct() {}
public function get($key) {
return isset($_GET[$key]) ? $_GET[$key] : null;
}
public function post($key) {
return isset($_POST[$key]) ? $_POST[$key] : null;
}
public function url() {
$http = 'http'.(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 's': '');
return $http.'://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
}
}
在中注冊課程twig
添加類實體的最簡單方法就是將其Request添加為全域
<?php
...
$twig->addGlobal('app', [ 'request' => new Request(), ]);
訪問模板內的類
<p>this {{ app.request.get('brand') }} bike is pretty awesome and we can deliver this at {{ app.request.get('location') }}. This {{ app.request.get('brand') }} is really trustworthy</p>
強制性說明
請注意,客戶端可能會嘗試注入/生成不安全的輸出。但是twig,只要您不使用例如過濾器將內容標記為安全,就會阻止這種情況raw
uj5u.com熱心網友回復:
Javascript
您的 javascript 的問題可能是因為您一遍又一遍地創建具有相同 id 的元素,這就是為什么只有具有正確 id 的第一個元素會被正確替換
一個簡單的解決方案是切換到類
var foo = 'foo';
var bar = 'bar';
document.querySelectorAll('.foo').forEach(span => {
span.innerHTML = foo;
});
document.querySelectorAll('.bar').forEach(span => {
span.innerHTML = bar;
});
span {
display: block;
}
span span {
margin: 10px 0 0 0;
}
span.foo {
color: red;
}
span.bar {
color: green;
}
<span class="foo"></span>
<span class="foo"></span>
<span class="bar"></span>
<span class="foo"></span>
<span class="foo"></span>
<span class="bar"></span>
uj5u.com熱心網友回復:
我可能會誤解,但 OP 似乎問了兩件事:(1)如何從 url 中提取資訊——無論是從路徑中還是從查詢中,(2)如何使用該資訊更改 dom。
看(1),可以從運算式中得到很多資訊new URL(window.location.href);
// using the path
// these window-named vars to simulate the browsers' window global
let windowA = {
location: {
href: "http://example.com/specialist/mybrand/mylocation"
}
};
const urlA = new URL(windowA.location.href);
console.log(urlA.pathname.split('/').slice(-2));
// using the query
let windowB = {
location: {
href: "http://example.com/specialist/?brand=mybrand&location=mylocation"
}
};
const urlB = new URL(windowB.location.href);
const params = [urlB.searchParams.get('brand'), urlB.searchParams.get('location')]
console.log(params);
看 (2),OP 代碼看起來不錯,除了 div 的 id 需要放在引號中......
let _window = {
location: {
href: "http://example.com/specialist/mybrand/mylocation"
}
};
const url = new URL(_window.location.href);
const components = url.pathname.split('/').slice(-2);
document.getElementById("brand").innerHTML = `<h1>${components[0]}</h1>`;
document.getElementById("location").innerHTML = `<h1>${components[1]}</h1>`;
<!-- notice the quoted id expressions -->
<div id="brand"></div>
<div id="location"></div>
uj5u.com熱心網友回復:
樹枝唯一的解決方案: https ://www.url.com/service/bikespecialist/trek/amsterdam/
{% set brand %}{{ request.url| split('/', 7)[5]| trim('/') }}{% endset %}
{% set location %}{{ request.url| split('/', 7)[6]| trim('/') }}{% endset %}
在文中
this {{ brand }} bike is awesome, come and buy it at {{ brand }} {{ location }}
感謝所有回復的人,非常感謝!在過去的幾周里,這個問題一直困擾著我
并且在網址中沒有品牌或位置的情況下
{% set band %}{{ request.url| split('/', 7)[5]| trim('/') |capitalize }}{% endset %}
{% if brand is empty %}
{% set brand %}qualitybikes {% endset %}
{% endif %}
{% set loation %}{{ request.url| split('/', 7)[6]| trim('/') |capitalize }}{% endset %}
{% if location is empty %}
{% set location %}entire country {% endset %}
{% endif %}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/478177.html
標籤:javascript 网址 枝条
