[英]How can I pull title of current page into a URL/src?
I'm looking to pull the title of the current page into an img src URL.我希望将当前页面的标题拉入 img src URL。
For instance:例如:
<title>booth106</title>
<img src="https://mywebsite.com/booth106/images/picture1.jpg">
Is there any code to put in so that as the title changes from page to page, so too will the booth number in the URL automatically.是否有任何代码可以输入,以便随着标题逐页更改,URL 中的展位号也会自动更改。 Is there a way to do this within a URL?有没有办法在 URL 中做到这一点?
You can do it like this.你可以这样做。 When the document is ready it will find the page title and edit the url of the picture!文档准备好后会找到页面标题并编辑图片的url!
$('img').attr('src', 'https://www.mywebsite.com/'+ document.title +'/images/picture1.jpg');
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <img src="https://www.mywebsite.com/booth106/images/picture1.jpg" alt="my image" />
var src='https://mywebsite.com/' + document.title + '/images/picture1.jpg';
var img = document.getElementsByTagName('img')[0]; //considering you want to update the src of the first image
img.src = src;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.