首页 新闻 会员 周边

vs中调试提示js缺少对象,作为html文件时候运行正确

0
悬赏园豆:50 [已解决问题] 解决于 2014-10-20 16:31

html文件可直接运行

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript">
function getImgSrc(file) {

document.getElementById('imgUp').src = file.value;
}
</script>
<style type="text/css">
.t1
{
height: 30px;
width: 280px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td class="t1">
</td>
<td rowspan="2">
<img src="" id="imgUp" style="height: 60px; width: 60px;" />
</td>
</tr>
<tr>
<td class="t1">
<input id='fileUp' name="" type="file" style=" width:270px;" onchange="getImgSrc(this)" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>

 

 

转换成aspx文件在vs中调试运行出错

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>

<!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 runat="server">
<title></title>

<style type="text/css">
.t1
{
height: 30px;
width: 280px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td class="t1">
</td>
<td rowspan="2">
<img src="" id="imgUp" style="height: 60px; width: 60px;" />
</td>
</tr>
<tr>
<td class="t1">
<input id='fileUp' name="" type="file" style=" width:270px;" onchange="getImgSrc(this)" />
</td>
</tr>
</table>
</div>
</form>
<script type="text/javascript">
function getImgSrc(file) {

document.getElementById('imgUp').src = file.value;
}
</script>
</body>
</html>

忧愁的主页 忧愁 | 初学一级 | 园豆:2
提问于:2012-06-14 10:59
< >
分享
最佳答案
0

貌似你这个是想实现一个图片上传预览的功能是吧,这种方法已经过时了,IE6+及其他浏览器都已经不支持这种方式了。

收获园豆:50
psforever | 菜鸟二级 |园豆:461 | 2012-06-14 13:08

那现在一般怎么实现?

忧愁 | 园豆:2 (初学一级) | 2012-06-14 13:10

@忧愁: 如果想兼容所有主流浏览器现在已经没办法在不将图片传到服务器上而可以预览,可以在onchange事件中用ajax将图片上传到服务器,然后就可以实现浏览了。

psforever | 园豆:461 (菜鸟二级) | 2012-06-14 21:06
其他回答(3)
0

检查下你输出的 HTML代码。

script脚本最好放在HEAD里,不过,你这个问题貌似跟这个没有关系。

无之无 | 园豆:5095 (大侠五级) | 2012-06-14 11:01
0

错误信息是什么?

artwl | 园豆:16736 (专家六级) | 2012-06-14 11:17

支持(0) 反对(0) 忧愁 | 园豆:2 (初学一级) | 2012-06-14 11:20

@忧愁: 可以查看到具体错误信息的,或换用firefox或chrome然后可以看到是哪儿出错了

支持(0) 反对(0) artwl | 园豆:16736 (专家六级) | 2012-06-14 11:26
0

把 function getImgSrc(file) 移至 <img src="" id="imgUp" style="height: 60px; width: 60px;" /> 之前试试

dudu | 园豆:30994 (高人七级) | 2012-06-14 11:41
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册