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

2024-12-12 06:47:19
推荐回答(2个)
回答(1):

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 ;页面中有

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

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

回答(2):

呵呵兄弟你是不是想把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)

}
呵呵兄弟看看我说的对不对,