小程序目的:在页面创建一个按钮,按此按钮调用createNewButton()函数创建一个新的按钮,按新的按钮调用show()函数在页面显示“new button content..."。
实际创建新按钮后创建属性setAttribute("onclick","show()"),使用ie浏览器打开,显示新按钮的onclick属性不起作用,为什么?如利用setAttribute创建的属性调用其他函数,应如何写?<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>attrTest</title>
<script>
function createNewButton(){
var body=document.getElementById("body");
var newBtn=document.createElement("input");
newBtn.setAttribute("type","button");
newBtn.setAttribute("value","show text");
newBtn.setAttribute("onclick","show()");//此句似乎在ie浏览器不起作用,无法调用show()函数
body.appendChild(newBtn);
}
function show(){
document.getElementById("txt").innerHTML="new button content....";
}
</script>
</head>
<body id="body">
<p id="txt">new button text show here</p>
<input type="button" onclick="createNewButton()" value="Create new button"/>
</body>
</html>
实际创建新按钮后创建属性setAttribute("onclick","show()"),使用ie浏览器打开,显示新按钮的onclick属性不起作用,为什么?如利用setAttribute创建的属性调用其他函数,应如何写?<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>attrTest</title>
<script>
function createNewButton(){
var body=document.getElementById("body");
var newBtn=document.createElement("input");
newBtn.setAttribute("type","button");
newBtn.setAttribute("value","show text");
newBtn.setAttribute("onclick","show()");//此句似乎在ie浏览器不起作用,无法调用show()函数
body.appendChild(newBtn);
}
function show(){
document.getElementById("txt").innerHTML="new button content....";
}
</script>
</head>
<body id="body">
<p id="txt">new button text show here</p>
<input type="button" onclick="createNewButton()" value="Create new button"/>
</body>
</html>
谢谢,通过了。能说的详细一点吗:
1、这两句都可以:newBtn.setAttribute("type","button");
newBtn.setAttribute("value","show text");
这个为什么就不行:newBtn.setAttribute("onclick","show()");
2 newBtn.onclick=show可以,newBtn.onclick=show();不行?
还望赐教啊。
我试过newBtn.setAttribute("onclick",show());不行,为什么去掉()就可以了,还请再多指教一点。
谢谢,通过了。能说的详细一点吗:
1、这两句都可以:newBtn.setAttribute("type","button");
newBtn.setAttribute("value","show text");
这个为什么就不行:newBtn.setAttribute("onclick","show()");
2 newBtn.onclick=show可以,newBtn.onclick=show();不行?
还望赐教啊。
3、本例实际由其他程序简化而来,那个程序的setAttribute(..,..)第二个参数调用的函数带参数,形如
newBtn.setAttribute("onclick","show(this)")或者newBtn.setAttribute("onclick","show(newBtn)")等等,这种情况应该怎么写?
我试过newBtn.setAttribute("onclick",show());不行,为什么去掉()就可以了,还请再多指教一点。
function a (){ alert(1) }var s = a;
var s = a();你觉得区别在那里呢?
谢谢,通过了。能说的详细一点吗:
1、这两句都可以:newBtn.setAttribute("type","button");
newBtn.setAttribute("value","show text");
这个为什么就不行:newBtn.setAttribute("onclick","show()");
2 newBtn.onclick=show可以,newBtn.onclick=show();不行?
还望赐教啊。
3、本例实际由其他程序简化而来,那个程序的setAttribute(..,..)第二个参数调用的函数带参数,形如
newBtn.setAttribute("onclick","show(this)")或者newBtn.setAttribute("onclick","show(newBtn)")等等,这种情况应该怎么写?var tt = this;
newBtn.setAttribute("onclick",function(){
show(tt);
});
谢谢,通过了。能说的详细一点吗:
1、这两句都可以:newBtn.setAttribute("type","button");
newBtn.setAttribute("value","show text");
这个为什么就不行:newBtn.setAttribute("onclick","show()");
2 newBtn.onclick=show可以,newBtn.onclick=show();不行?
还望赐教啊。
3、本例实际由其他程序简化而来,那个程序的setAttribute(..,..)第二个参数调用的函数带参数,形如
newBtn.setAttribute("onclick","show(this)")或者newBtn.setAttribute("onclick","show(newBtn)")等等,这种情况应该怎么写?var tt = this;
newBtn.setAttribute("onclick",function(){
show(tt);
});
必须这样嵌套转换一下?没直接调用的办法?
function fun(){
...
var tr=table.insertRow(index);
var td=tr.insertCell(0);
var btn=document.createElement("input");
btn.setAttribute("type","button");
btn.setAttribute("value","new button");
btn.setAttribute("style","font-weight:bold");//这句不起作用
td.appendChild(btn);
...
}
但是创建出的按钮的字体不是黑体,为什么?
function fun(){
...
var tr=table.insertRow(index);
var td=tr.insertCell(0);
var btn=document.createElement("input");
btn.setAttribute("type","button");
btn.setAttribute("value","new button");
btn.setAttribute("style","font-weight:bold");//这句不起作用
td.appendChild(btn);
...
}
但是创建出的按钮的字体不是黑体,为什么?测试正常
function fun(){
...
var tr=table.insertRow(index);
var td=tr.insertCell(0);
var btn=document.createElement("input");
btn.setAttribute("type","button");
btn.setAttribute("value","new button");
btn.setAttribute("style","font-weight:bold");//这句不起作用
td.appendChild(btn);
...
}
但是创建出的按钮的字体不是黑体,为什么?测试正常我测试不出来:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>attrTest</title>
<script>
function createNewButton(){
var body=document.getElementById("body");
var newBtn=document.createElement("input");
newBtn.setAttribute("type","button");
newBtn.setAttribute("value","show text");
newBtn.setAttribute("onclick",show);
newBtn.setAttribute("style","font-weight:bold");//这句不起作用,想设成黑体
body.appendChild(newBtn);
}
function show(){
document.getElementById("txt").innerHTML="new button content....";
}
</script>
</head>
<body id="body">
<p id="txt">new button text show here</p>
<input type="button" onclick="createNewButton()" value="Create new button" style="font-weight:bold"/>//按钮文本显示为黑体
</body>
</html>