调用变量值以获取IMG源值

发布于 2025-02-06 10:29:32 字数 639 浏览 0 评论 0原文

因此,我是新来的哈巴狗和编程。我想获得IMG源并根据变量名称进行打印。我想做的是:

- var values = ['car', 'space', 'plants', 'rock', 'chair', 'phone', 'television'];

然后,我会做一个迭代此数组的迭代,并根据单词进行随机打印,例如:

each val in values.length ? values : ['There are no values']
  img(src="https://source.unsplash.com/featured/1200x900?"+val alt=val loading="lazy")

我尝试了不同的方法,但似乎不知道如何使用和打印图片帕格。我试图使用它来调用它:

img(src="https://source.unsplash.com/featured/1200x900?values[0]", alt="random")

但是只有我只是说:

img(src="https://source.unsplash.com/featured/1200x900?car", alt="random")

So I'm new with PUG and programming. I want to get img source and print it depending on the variable name. What I want to do is:

- var values = ['car', 'space', 'plants', 'rock', 'chair', 'phone', 'television'];

Then I'll do a for to iterate over this array and print random unplash depending on the word, something like:

each val in values.length ? values : ['There are no values']
  img(src="https://source.unsplash.com/featured/1200x900?"+val alt=val loading="lazy")

I have tried different ways but can't seem to know how to call and print pictures using PUG. I tried to call it using:

img(src="https://source.unsplash.com/featured/1200x900?values[0]", alt="random")

But only works if I just say:

img(src="https://source.unsplash.com/featured/1200x900?car", alt="random")

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猫性小仙女 2025-02-13 10:29:32

您的哈哈代码:

- var values = ['car', 'space', 'plants', 'rock', 'chair', 'phone', 'television'];
each val in values.length ? values : ['There are no values']
  img(src="https://source.unsplash.com/featured/1200x900?"+val alt=val loading="lazy")

生成HTML:

<img src="https://source.unsplash.com/featured/1200x900?car" alt="car" loading="lazy"/>
<img src="https://source.unsplash.com/featured/1200x900?space" alt="space" loading="lazy"/>
<img src="https://source.unsplash.com/featured/1200x900?plants" alt="plants" loading="lazy"/>
<img src="https://source.unsplash.com/featured/1200x900?rock" alt="rock" loading="lazy"/>
<img src="https://source.unsplash.com/featured/1200x900?chair" alt="chair" loading="lazy"/>
<img src="https://source.unsplash.com/featured/1200x900?phone" alt="phone" loading="lazy"/>
<img src="https://source.unsplash.com/featured/1200x900?television" alt="television" loading="lazy"/>

您期望什么?怎么了?

您可以在任何在线转换器中进行自我测试,例如: https://www.ubercompute .com/pug-to-html

Your Pug code:

- var values = ['car', 'space', 'plants', 'rock', 'chair', 'phone', 'television'];
each val in values.length ? values : ['There are no values']
  img(src="https://source.unsplash.com/featured/1200x900?"+val alt=val loading="lazy")

generate HTML:

<img src="https://source.unsplash.com/featured/1200x900?car" alt="car" loading="lazy"/>
<img src="https://source.unsplash.com/featured/1200x900?space" alt="space" loading="lazy"/>
<img src="https://source.unsplash.com/featured/1200x900?plants" alt="plants" loading="lazy"/>
<img src="https://source.unsplash.com/featured/1200x900?rock" alt="rock" loading="lazy"/>
<img src="https://source.unsplash.com/featured/1200x900?chair" alt="chair" loading="lazy"/>
<img src="https://source.unsplash.com/featured/1200x900?phone" alt="phone" loading="lazy"/>
<img src="https://source.unsplash.com/featured/1200x900?television" alt="television" loading="lazy"/>

What do you expect? What is wrong?

You can self test your Pug code in any online converter, e.g. here: https://www.ubercompute.com/pug-to-html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文