Javascript函数返回src路径 [英] Javascript function return src path

查看:144
本文介绍了Javascript函数返回src路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试通过调用一个javascript函数来设置一个src,返回的路径如下:

I'm trying to set an 'src' by calling a javascript function that returns the path like so:

<img src="getImagePath()" />

功能:

function getImagePath(){

     return "images/image1.png";

}

但它似乎无效。我遗漏了什么?

But it doesn't seem to work. Anything I'm missing? Thanks to anyone pointing me in the right direction.

推荐答案

src 属性采用URL,而不是JavaScript。您可以尝试

The src attribute takes an URL, not JavaScript. You might want to try

<img src="pixel.gif" onload="this.onload=null; this.src=getImagePath();" />

这篇关于Javascript函数返回src路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆