是否可以根据打开的网页的URL修改内容?例如,我有50个内容略有不同的克隆页面,假设每一行都是不同页面的内容
<!--Page Paris-->
<span class="address">Street 1, House 1</span>
<span class="hours">10:00-18:00</span>
<!--Page Berlin-->
<span class="address">Street 2, House 2</span>
<span class="hours">10:00-19:00</span>
<!--Page London-->
<span class="address">Street 3, House 3</span>
<span class="hours">10:00-20:00</span>我需要从脚本中删除内容:
类似
<span class="address">Street 1, House 1</span>
<span class="hours">10:00-18:00</span>如果页面地址为https://mywebsite.com/berlin/,则为
<span class="address">Street 2, House 1</span>
<span class="hours">10:00-19:00</span>如果页面地址为https://mywebsite.com/london/,则为
<span class="address">Street 3, House 3</span>
<span class="hours">10:00-20:00</span>这样,我只需要一个页面,而不是50个页面,内容将只依赖于页面URL。提前谢谢你的帮助。
发布于 2020-11-19 09:03:00
是的,通过使用javascript脚本来处理window.location对象是可能的。
我的意思是,如果您有这样的链接,您就可以获得当前的站点路径
let mypath = window.location.pathname // this returns 'ccc' if you are on https://mywebsite.com/ccc/所以您可以添加这样的脚本
<script type="text/javascript">
window.addEventListener('load', start);
function start(){
let mypath = window.location.pathname; // get the current site path
let span = document.querySelector(".address"); // get the span object
span.innerText = mypath; // set the span text with the current path
}
</script>要添加这个小时,您必须以类似的方式操作,但要使用javascript中的Date。
编辑
如果要在span中而不是当前url路径中添加预定义文本,则可以定义包含文本的对象。例如
<script type="text/javascript">
window.addEventListener('load', start);
const myTexts{
'/path_of_the_site/longer_path' : 'custom text',
'/paris': ' paris via',
'/spain': 'spain via'
}
function start(){
let myPath = window.location.pathname; // get the current site path
span.innerText = myTexts[myPath]; // set the span text from the object based on the path
}
</script>https://stackoverflow.com/questions/64908349
复制相似问题