有如下代码
<!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>
<title>无标题页</title> <script type="text/javascript">
function ff(){
var a=document.createElement("a");
a.onclick=cc;
a.appendChild(document.createTextNode("d"));
document.body.appendChild(a);
//debugger
}
function cc(u){
alert(u);
}
</script></head>
<body onload="ff()">
</body>
</html>怎样才能给cc传递参数"u"?
我不像使用全局变量,因为使用全局变量可能会导致出错。
<!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>
<title>无标题页</title> <script type="text/javascript">
function ff(){
var a=document.createElement("a");
a.onclick=cc;
a.appendChild(document.createTextNode("d"));
document.body.appendChild(a);
//debugger
}
function cc(u){
alert(u);
}
</script></head>
<body onload="ff()">
</body>
</html>怎样才能给cc传递参数"u"?
我不像使用全局变量,因为使用全局变量可能会导致出错。
function ff(){
var a=document.createElement("a");
a.onclick=cc("dsfsd");//这样不就行了吗?
a.appendChild(document.createTextNode("d"));
document.body.appendChild(a);
//debugger
}
function cc(u){
alert(u);
}
</script>
function ff(){
var a=document.createElement("a");
a.onclick=cc("dsfsd");//这样不就行了吗?
a.appendChild(document.createTextNode("d"));
document.body.appendChild(a);
//debugger
}
function cc(u){
alert(u);
}
</script>如果是创建<a>标签可以这样
<script type="text/javascript">
function ff(u){
var a=document.createElement("a");
a.href=u;
a.appendChild(document.createTextNode("ddd"));
document.body.appendChild(a);
//debugger
} </script></head>
<body onload="ff('http://www.baidu.com')">
<!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>
<title>无标题页</title> <script type="text/javascript">
function ff(){
var a=document.createElement("a");
(
function(arg)
{
a.onclick=function(){cc(arg);};
}
)("aaaa");
a.appendChild(document.createTextNode("d"));
document.body.appendChild(a);
//debugger
}
function cc(u){
alert(u);
}
</script></head>
<body onload="ff()">
</body>
</html>
a.onclick = function(u){ cc(u);};
a.onclick = function(){ cc(u);};
但是我要想动态的创建多个a标签,每个标签的onclick事件都是一样的,就是参数不同。怎么实现?
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>无标题页</title> <script type="text/javascript">
function ff(u){
var a=document.createElement("a");
a.onclick=function(){
cc(u);
};
a.appendChild(document.createTextNode(",,,,,,,,,,,,,,,,d"));
document.body.appendChild(a);
//debugger
}
function cc(u){
alert(u);
}
</script></head>
<body onload="ff('55555555')">
</body>
</html>