JS中document.getElementById("id").innerHTML之中的id如何用变量表示

<title>无标题文档</title><scriptlanguage="javascript">varstr_span;functionmain(str_value,s... <title>无标题文档</title>
<script language="javascript">
var str_span;
function main(str_value,str_span)
{

alert(str_span);
//alert(document.getElementById(ajax));
alert(document.getElementById("ajax").innerHTML);
alert("str_span");
//alert(document.test.str_span);
//s = 'str_span';
if(str_value.length==0)
{
alert('用户名为空')
document.getElementById("str_span").innerHTML ='123';
return
}
xmlHttp = GetxmlHttpObject();
if(xmlHttp == null)
{
alert('你的浏览器不支持AJAX');
return ;
}
//else{alert('pass')}
var url = 'php.php'+'?q='+str_value+'&sid='+Math.random();
//alert(url);
xmlHttp.onreadystatechange = stateChange;
xmlHttp.open("GET",url,true);
xmlHttp.send(null)

}
function stateChange()
{
if(xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{
//str_span = xmlHttp.responseText;
document.getElementById("str_span").innerHTML= xmlHttp.responseText;
}
}
function GetxmlHttpObject()
{
var xmlHttp = null;
try
{
xmlHttp = new XMLHttpRequest();
}
catch(e)
{
try
{
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e)
{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}

</script>
</head>

<body>
<table border="1">
<td>dfwfgdg</td>
<form name="test">
<tr><td><input type="text" name="username" />
</td><td><input type="button" value="检查用户名是否被占用" name="username_used_check" onclick="main(document.test.username.value,ajax)" />
<td><span id="ajax" name="ajax"></span>
<td><span id="fire"></span>
</td></tr></form>
</table>
</body>
</html>

到底应该怎么改才能实现一个main()函数接收相应变量来实现异步刷新功能
main(str_value,str_span)中一个代表输入的<INPUT>中的值,一个代表输出在<SPAN>的值
展开
 我来答
大野瘦子
高粉答主

2018-12-11 · 繁杂信息太多,你要学会辨别
知道小有建树答主
回答量:1227
采纳率:100%
帮助的人:27万
展开全部

function main(str_value,str_span)

{

//alert(str_span);

var str_spanvar = str_span;

//alert(document.getElementById(str_spanvar));

//alert(document.getElementById(str_spanvar).innerHTML);

//alert("str_span");

//alert(document.test.str_span);

//s = 'str_span';

if(str_value.length==0)

{

alert('用户名为空')

document.getElementById(str_spanvar).innerHTML ='123';

return

}

xmlHttp = GetxmlHttpObject();

if(xmlHttp == null)

{

alert('你的浏览器不支持AJAX');

return ;

}

//else{alert('pass')}

var url = 'php.php'+'?q='+str_value+'&sid='+Math.random();

//alert(url);

xmlHttp.onreadystatechange = stateChange;

xmlHttp.open("GET",url,true);

xmlHttp.send(null)

}

扩展资料:

js document.getElementById的用法详解

语法:oElement = document . getElementById ( sID )

参数:sID――必选项。字符串 (String) 。

返回值:oElemen――对象 (Element) 。

说明:根据指定的 id 属性值得到对象。返回 id 属性值等于 sID 的第一个对象的引用,假如对应的为一组对象,则返回该组对象中的第一个,如果无符合条件的对象,则返回 null 。

注意: 

document.getElementById(" ") 得到的是一个对象,用 alert 显示得到的是“ object ”,而不是具体的值,它有 value 和 length 等属性,加上 .value 得到的才是具体的值!

细节:

1、 document.getElementById 有时会抓 name 放过了 id ,据说是 IE 的一个 BUG ;页面中有

<input type="hidden" id="hello8" name="category_id" value="2" />

<select id="category_id" onchange="al();">

一个是name="category_id" 一个是id="category_id",用document.getElementById取第二个,可是,取到的却是第一个name=category_id,在IE中getElementById竟然不是先抓id而是先找name相同的物件。

2、javascript中的getElementbyId使用

网页中的元素必须有id属性,才能通过这个方法得到,比如<input type=text name="content" id="content">

3、获取html标记主要有两种方法,一种是通过ID值,一种是通过name属性(name属性主要用于form表单内的input标记。) 

document.getElementById(" ") 得到的是一个对象,用 alert 显示得到的是
“ object ”,而不是具体的值,它有 value 和 length 等属性,加上 .value 得到的才
是具体的值

风清云中子
2010-01-26 · TA获得超过357个赞
知道小有建树答主
回答量:128
采纳率:0%
帮助的人:147万
展开全部
呵呵兄弟你是不是想把str_span变量动态放在document.getElementById("id").innerHTML中的id位置吧,js函数里好像是不支持直接把函数里的变量放在系统函数或类里吧,应该先赋值下,这样就可以了,
function main(str_value,str_span)
{

//alert(str_span);
var str_spanvar = str_span;
//alert(document.getElementById(str_spanvar));
//alert(document.getElementById(str_spanvar).innerHTML);
//alert("str_span");
//alert(document.test.str_span);
//s = 'str_span';
if(str_value.length==0)
{
alert('用户名为空')
document.getElementById(str_spanvar).innerHTML ='123';
return
}
xmlHttp = GetxmlHttpObject();
if(xmlHttp == null)
{
alert('你的浏览器不支持AJAX');
return ;
}
//else{alert('pass')}
var url = 'php.php'+'?q='+str_value+'&sid='+Math.random();
//alert(url);
xmlHttp.onreadystatechange = stateChange;
xmlHttp.open("GET",url,true);
xmlHttp.send(null)

}
呵呵兄弟看看我说的对不对,
本回答被提问者和网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式