[英]javascript function is not being called if i pass a argument
the function setImg() if called with an argument is not running but if no argument is passed then the function runs what mi doing wrong please help. setImg()函数(如果使用参数调用)未运行,但如果未传递参数,则该函数运行mi做错了的事情,请帮忙。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<script type="text/javascript">
function setImg(p)
{
window.alert(p);
document.getElementById('img').innerHTML ="<img src=p width='100' height='105'>";
}
</script>
</head>
<body>
<a href="#" onclick="setImg("images/user-icon.png");">load image</a>
<div id="img">
</div>
</body>
</html>
You are missing the quotes for the src plus you should break up the string and add the variable to prevent it reading p
as text. 您缺少src的引号,还应该分解字符串并添加变量以防止其将
p
读为文本。
Update the JS to the following: 将JS更新为以下内容:
<script type="text/javascript">
function setImg(p)
{
document.getElementById('img').innerHTML ="<img src='" + p + "' width='100' height='105'>";
}
</script>
Plus in the HTML you need to be careful with quotes: 另外,在HTML中,您需要注意引号:
<a href="#" onclick="setImg('images/user-icon.png');">load image</a>
Small mistake of quotes 小报价错误
change 更改
<a href="#" onclick="setImg("images/user-icon.png");">load image</a>
to 至
<a href="#" onclick="setImg('images/user-icon.png');">load image</a>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<script type="text/javascript">
function setImg(p)
{
window.alert(p);
document.getElementById('img').innerHTML ="<img src=p width='100' height='105'>";
}
</script>
</head>
<body>
<a href="#" onclick="setImg('images/user-icon.png');">load image</a>
<div id="img">
</div>
</body>
</html>
Well your were closing html attribute accidentally, you should use '
single-quotes instead. 好吧,您不小心关闭了html属性,应该改用
'
单引号。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.