添加一个onclick事件 [英] add an onclick event

查看:99
本文介绍了添加一个onclick事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨大师


我有以下代码:


函数CI(ImgN,Alt){

if(document.getElementById&& document.getElementById(''il'')){


var d = document.getElementById(''il'' );


if(document.createElement){


var i = document.createElement(''img'');


i.src =" i /" + ImgN;


i.className =''l'';


i.alt = Alt;

while(d.firstChild!== null){


d.removeChild(d.firstChild);


}


d.appendChild(i);


返回false;


}否则如果( d.innerHTML){


d.innerHTML =''< img src =" i /''+ ImgN +''"类= >> 1" alt ="''+ Alt +''"

ONCLICK =" return XI()">'';


返回false ;


}否则{


返回true;}


}其他{


返回true;}


}


我现在想要将onclick事件添加到img(这样用户就可以关闭它了,我该怎么做,或者我可以简单地在底部添加一些文字

图像''关闭图像''(我该怎么办?)?


TIA


- Nicolaas

解决方案

2004年11月17日星期三23:16:41 +1300,WindAndWaves< ac **** @ ngaru.com>写道:

我有以下代码:


您可以在发布时缩进代码。这很难读,

否则。不过,我对它有一些评论。


第一个这样的评论是你的返回值是倒退的。我假设

因为你想要取消一个

事件而你成功返回false。在我看来,这个函数应该只涉及它自己 - 如果在更高级别的
,你需要相反的结果,使用NOT逻辑运算符

(!)函数已返回。

函数CI(ImgN,Alt){
if(document.getElementById&& document.getElementById(''il'')){
var d =的document.getElementById( '' IL '');


这个效率很低。首先要做的是打电话给

gEBI可靠。简单来说,那就是:


if(!document.getElementById){

document.getElementById = function(){return null;} ;

}


使用document.all进行仿真可以在FAQ注释[1]中找到,并在

帖子中找到我过去。


然后您可以将前两行更改为:


var d = document.getElementById(''il' ');

if(d){

if(document.createElement){


if(document.createElement& & d.appendChild&& d.removeChild){

var i = document.createElement(''img'');
i.src =" i /" + ImgN;
i.className =''l'';
i.alt = Alt;

while(d.firstChild!== null){


严格的比较是没有必要的,也可能是有害的 - 如果
d.firstChild未定义,循环的内容将被执行。

标准不等运算符(!=)将避免这种情况。

d.removeChild(d.firstChild);
}
d.appendChild( i);
返回false;
}如果(d.innerHTML){


这不是足够的测试。即使d.innerHTML是一个字符串


}否则if(''string''== typeof d.innerHTML){


那里不保证写入它会产生任何影响。常见问题

注释[1]显示正确的测试。

d.innerHTML =''< img src =" i /''+ ImgN +''" ;类= >> 1" alt ="''
+ Alt +''" ONCLICK ="返回XI()">'';
返回false;
} else {
return true;
}
} else {
返回true;
}
}


最后的改进是将函数改为

只执行一次路径,一旦支持得到证实。但是,'b
可能过多。

我现在想要将onclick事件添加到img(以便用户可以关闭它),




innerHTML路径似乎可以实现这一目标。为了实现相同的

DOM路径,添加


i.onclick = function(){return XI();};


[snip]


希望有所帮助,

Mike

[1]注意:<网址:http://www.jibbering.com/faq/faq_notes/faq_notes.html>


-

Michael Winter

替换.invalid与.uk通过电子邮件回复。


Michael Winter写道:

[...]

功能CI(ImgN,Alt){


[...]

+ Alt +''" ONCLICK ="返回XI()">'';



[...]

i.onclick = function(){return XI ();};




如果打算将CI()函数添加到替换图像,

该行应为:


i.onclick = function(){return CI();};


或是XI()在其他地方定义了吗? />

干杯,Rob


2004年11月17日星期三21:39:50 +1000,RobG< rg *** @ iinet.net.auau>写道:

Michael Winter写道:




[snip]

< blockquote class =post_quotes> i.onclick = function(){return XI();};



如果打算将CI()函数添加到替换
图像,这行应该是:

i.onclick = function(){return CI();};

或是XI()在其他地方定义了吗? / blockquote>


我的解释是通过点击某些东西来调用CI,它就是
在文档中添加一个图像。 OP还想要一种方法来删除

图像:XI。调用XI的方法是通过innerHTML路径提供的,但

不是DOM路径。


我可能错了。我们只能拭目以待。


迈克


-

Michael Winter

替换.invalid与.uk通过电子邮件回复。


Hi Gurus

I have the following code:

function CI(ImgN, Alt){

if( document.getElementById && document.getElementById(''il'') ){

var d=document.getElementById(''il'');

if(document.createElement){

var i=document.createElement(''img'');

i.src="i/" + ImgN;

i.className=''l'';

i.alt=Alt;

while(d.firstChild!==null){

d.removeChild(d.firstChild);

}

d.appendChild(i);

return false;

}else if(d.innerHTML){

d.innerHTML=''<img src="i/'' + ImgN + ''" class="l" alt="'' + Alt + ''"
ONCLICK="return XI()">'';

return false;

}else {

return true;}

} else {

return true;}

}

I now want to add the onclick event to the img (so that the user can close
it), how do I do that, or can I simply add a bit of text at the bottom of
the image ''close image'' (and how do I do that??)?

TIA

- Nicolaas

解决方案

On Wed, 17 Nov 2004 23:16:41 +1300, WindAndWaves <ac****@ngaru.com> wrote:

I have the following code:
Could you please indent code when you post it. It''s difficult to read,
otherwise. I have some comments for it, though.

The first such comment is that your return values are backwards. I assume
that you''re returning false upon success because you want to cancel an
event. In my opinion, the function should concern only itself - if, at a
higher level, you need the opposite result, use the NOT logical operator
(!) once the function has returned.
function CI(ImgN, Alt){
if(document.getElementById && document.getElementById(''il'')) {
var d=document.getElementById(''il'');
This is rather inefficient. The first thing to do would be make a call to
gEBI reliable. At a simple level, that''s:

if(!document.getElementById) {
document.getElementById = function() {return null;};
}

Emulation using document.all can be found in the FAQ notes[1], and in
posts from me in the past.

You could then change the first two lines to:

var d = document.getElementById(''il'');
if(d) {
if(document.createElement) {
if(document.createElement && d.appendChild && d.removeChild) {
var i=document.createElement(''img'');
i.src="i/" + ImgN;
i.className=''l'';
i.alt=Alt;

while(d.firstChild!==null){
A strict comparison isn''t necessary, and could be harmful - if
d.firstChild is undefined, the contents of the loop will be executed. The
standard inequality operator (!=) will avoid that.
d.removeChild(d.firstChild);
}
d.appendChild(i);
return false;
} else if(d.innerHTML) {
This isn''t a sufficient test. Even if d.innerHTML was a string

} else if(''string'' == typeof d.innerHTML) {

there''s no guarantee that writing to it will have any effect. The FAQ
notes[1] show a proper test.
d.innerHTML=''<img src="i/'' + ImgN + ''" class="l" alt="''
+ Alt + ''" ONCLICK="return XI()">'';
return false;
} else {
return true;
}
} else {
return true;
}
}
The final improvement would be to have the function alter itself to
execute only one path, once support has been proven. However, that''s
probably excessive.
I now want to add the onclick event to the img (so that the user can
close it),



The innerHTML path seems to accomplish that. To accomplish the same for
the DOM path, add

i.onclick = function() {return XI();};

[snip]

Hope that helps,
Mike
[1] Notes: <URL:http://www.jibbering.com/faq/faq_notes/faq_notes.html>

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.


Michael Winter wrote:
[...]

function CI(ImgN, Alt){
[...]
+ Alt + ''" ONCLICK="return XI()">'';


[...]
i.onclick = function() {return XI();};



If the intention is to add the CI() function to the replacement image,
should this line be:

i.onclick = function() {return CI();};

or is XI() defined somewhere else?

Cheers, Rob


On Wed, 17 Nov 2004 21:39:50 +1000, RobG <rg***@iinet.net.auau> wrote:

Michael Winter wrote:



[snip]

i.onclick = function() {return XI();};



If the intention is to add the CI() function to the replacement
image, should this line be:

i.onclick = function() {return CI();};

or is XI() defined somewhere else?



My interpretation was that CI was called by clicking something, and it
adds an image to the document. The OP also wants a way to remove that
image: XI. A way to call XI is provided through the innerHTML path, but
not the DOM path.

I could be wrong. We''ll just have to wait and see.

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.


这篇关于添加一个onclick事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆