带参数的Javascript警报

时间:2017-12-12 05:41:18

标签: javascript html function parameters alert

我试图在javascript中创建一个函数并传入参数" name",然后当用户点击一张照片时,一个警告会说出像这样的照片#34;这张照片是____"

<img src="photos/PhotoVersailles.jpg" onclick="photoWhere(Versailles)" style="width:100%">

<script type="text/javascript">
function photoWhere(name)
    {
        alert("This photo was taken in "+name+".");
    }
</script>

为什么我的尝试不起作用?

3 个答案:

答案 0 :(得分:0)

尝试凡尔赛&#39;作为参数传递时

&#13;
&#13;
function photoWhere(name) {
  alert("This photo was taken in " + name + ".");
}
&#13;
<img src="photos/PhotoVersailles.jpg" onclick="photoWhere(' Versailles')" alt="not found" style="width:100%">
&#13;
&#13;
&#13;

答案 1 :(得分:0)

语法错误,请将警报更改为:

var nameStr = "This photo was taken in "+name+".";
alert(nameStr);

答案 2 :(得分:0)

您可以使用此:

function photoWhere(Pic) { var _pic = Pic.name; _pic = "This photo was taken in "+_pic+"." alert(_pic); }
<img src="photos/PhotoVersailles.jpg" onclick="photoWhere(this)" 
name="Versailles" style="width:50%">