使用setAttribute添加onclick事件 [英] add onclick event using setAttribute
问题描述
全部,
我试图使用setAttribute将onclick事件添加到< tdfrom
javascript。显然代码似乎不适用于FF3,但在IE7中工作得非常好。
以下是您参考的片段:
< ...
var _table = document.createElement(" table");
_tbody = document.createElement(" tbody");
var _tr = document.createElement(" tr");
var _td = document.createElement(" td");
_td。 innerHTML =" 6";
_td.id ="" + x +" _" + y +"" ;;
_td.setAttribute( " onclick",function(){check(event);});
_tr.appendChild(_td);
_tbody.appendChild(_tr);
_table.appendChild(_tbody");
....
All,
I was trying to add an onclick event using setAttribute to a <tdfrom
javascript. Apparently the code doesnot seem to work in FF3 but works
pretty well in IE7.
Here is the snippet for ur reference:
<...
var _table=document.createElement("table");
_tbody=document.createElement("tbody");
var _tr = document.createElement("tr");
var _td = document.createElement("td");
_td.innerHTML="6";
_td.id=""+x+"_"+y+"";
_td.setAttribute("onclick",function(){check(event) ;});
_tr.appendChild(_td);
_tbody.appendChild(_tr);
_table.appendChild(_tbody");
....
>
>
这里是函数Check的定义
功能检查(e){
if(!e)
{
var e = window。事件;
alert(e +":None");
}
if(e.target)
{
targ = e.target;
alert(" FF / Netscape:");
}
else if(e.srcElement)
{
targ = e.srcElement;
alert(" IE");
document.getElementById(e.srcElement.id).innerHTML =" 0";
}
alert(targ .tagName);
}
跟踪此问题的任何帮助都非常有用。
And here is function "Check''s" definition
function check(e){
if (!e)
{
var e=window.event;
alert(e+" :None");
}
if (e.target)
{
targ=e.target;
alert("FF/Netscape: ");
}
else if (e.srcElement)
{
targ=e.srcElement;
alert("IE");
document.getElementById(e.srcElement.id).innerHTML ="0";
}
alert(targ.tagName);
}
Any help in tracking this problem would be really useful.
推荐答案
7月19日,1:49 * pm,Sri02< sri.ram ... @ gmail.comwrote:
On Jul 19, 1:49*pm, Sri02 <sri.ram...@gmail.comwrote:
>
_td.setAttribute(" onclick",function(){check(event);});
>
_td.setAttribute("onclick",function(){check(event) ;});
_td.onclick = check;
--Jorge。
_td.onclick= check;
--Jorge.
Sri02写道:
Sri02 wrote:
我试图使用setAttribute将onclick事件添加到< tdfrom
javascript。显然,代码似乎不适用于FF3,但在IE7中工作得非常好。
I was trying to add an onclick event using setAttribute to a <tdfrom
javascript. Apparently the code doesnot seem to work in FF3 but works
pretty well in IE7.
setAttribute()有很多错误的实现,避免它。
setAttribute() has a number of buggy implementations, avoid it.
这是以下代码段你的参考:
Here is the snippet for ur reference:
^^
这不是网络聊天。
^^
This is not an Internet chat.
< ...
var _table = document.createElement(" table");
_tbody = document.createElement(" tbody");
var _tr = document.createElement(" tr");
var _td = document.createElement(" td");
_td.innerHTML =" 6" ;;
<...
var _table=document.createElement("table");
_tbody=document.createElement("tbody");
var _tr = document.createElement("tr");
var _td = document.createElement("td");
_td.innerHTML="6";
使用专有的innerHTML属性不需要将元素的内容设置为6。
元素的内容为6。特别是当你使用
符合标准的DOM方法时。
_td.appendChild(document.createTextNode(" 6"));
Using the proprietary innerHTML property is not necessary for setting
the content of an element to "6". Especially not when you are using
standards-compliant DOM methods already.
_td.appendChild(document.createTextNode("6"));
_td.id ="" + x +" _" + y +"" ;;
_td.id=""+x+"_"+y+"";
前导和尾随连接效率低且多余。
_td.id = x +" _" + y;
The leading and trailing concatenation is inefficient and superfluous.
_td.id = x + "_" + y;
_td.setAttribute(" onclick",function(){check(event);});
_td.setAttribute("onclick",function(){check(event) ;});
正如Jorge所说。
As Jorge said.
_tr.appendChild(_td);
_tbody.appendChild(_tr);
_table.appendChild(_tbody");
_tr.appendChild(_td);
_tbody.appendChild(_tr);
_table.appendChild(_tbody");
还有一个额外的<"字符。
There is an extra <"character.
...
...
所有这一切都应该在使用前通过运行时功能测试来保护,当然:
if(_td&& isMethod(_td) ,appendChild))
{
// ...
}
All of this should be secured by a runtime feature test before use, of course:
if (_td && isMethod(_td, "appendChild"))
{
// ...
}
这里是功能Check的定义
功能检查(e){
if(!e)
{
var e = window。事件;
alert(e +":None");
}
if(e.target)
And here is function "Check''s" definition
function check(e){
if (!e)
{
var e=window.event;
alert(e+" :None");
}
if (e.target)
如果(e)
{
var t}这应至少为
= e.target || e.srcElement;
if(t)
{
// ...
}
}
无需为每个DOM设置单独的分支。
HTH
PointedEars
-
任何拍下这个页面的人最好用浏览器X查看标签
a网页似乎是渴望过去的糟糕时光,在网络之前,当你几乎没有机会阅读另一台计算机,另一台文字处理器或其他网络上写的文件的时候,这就是b $ b。 - Tim Berners-Lee
This should be at least
if (e)
{
var t = e.target || e.srcElement;
if (t)
{
// ...
}
}
No need to have a separate branch for each DOM.
HTH
PointedEars
--
Anyone who slaps a ''this page is best viewed with Browser X'' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee
Thomas''PointedEars''Lahn写道:
Thomas ''PointedEars'' Lahn wrote:
Sri02写道:
Sri02 wrote:
> _td.setAttribute(" onclick",function(){check(event);});
>_td.setAttribute("onclick",function(){check(event );});
正如乔治所说。
As Jorge said.
JFTR,符合标准的方法是:
_td.addEventListener(" click",function(e) {check(e);},false);
或
_td.addEventListener(
" click",{handleEvent:function(e){check(e);}},false);
但MSHTML不支持它,而attachEvent()不是一个可行的选择。
PointedEars
-
var bugRiddenCrashPronePieceOfJunk =(
navigator.userAgent.indexOf('''MSIE 5' ')!= -1
&& navigator.userAgent.indexOf(''Mac'')!= -1
)// Plone,register_function.js :16
JFTR, the standards-compliant approach is:
_td.addEventListener("click", function(e) { check(e); }, false);
or
_td.addEventListener(
"click", {handleEvent: function(e) { check(e); }}, false);
But MSHTML does not support it and attachEvent() is not a viable alternative.
PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf(''MSIE 5'') != -1
&& navigator.userAgent.indexOf(''Mac'') != -1
) // Plone, register_function.js:16
这篇关于使用setAttribute添加onclick事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!