使用动态创建的元素 [英] Using Elements Created on the fly
问题描述
我使用javascript即时创建了一个元素:
myA = document.createElement(" A");
myA。 href =" javascript :acton(this)" ;;
myA.className =" smallLink";
myText = document.createTextNode (''sometext'');
myA.appendChild(myText);
然后当我按下链接时我想改变''这个
链接的'className''从''smallLink''到''greaterLink'',但没有任何反应。
函数动作(obj)< br $>
{
// alert(obj);
obj.className =" higherlink";
}
当我查看警报(obj)时,我得到[object]。
当我查看alert(obj.className)时,我得到了 ; undefined"。
任何人都知道我在这里做错了吗?
迈克
I created an element on-the-fly using javascript like:
myA=document.createElement("A");
myA.href="javascript:acton(this)";
myA.className = "smallLink";
myText=document.createTextNode(''sometext'');
myA.appendChild(myText);
Then when I pressed the link I''d like to change the ''className'' of this
link from ''smallLink'' to ''biggerLink'', but nothing happens.
function action(obj)
{
//alert(obj);
obj.className="biggerlink";
}
When I look at alert(obj) I get "[object]".
When I look at alert(obj.className) I get "undefined".
Anyone know what I am doing wrong here?
Mike
推荐答案
也许
返回obj.clas sName =" greaterlink";
-
George Hester
__________________________________
"迈克尔希尔 <喜**** @ ram.lmtas.lmco.com>在消息新闻中写道:40 *************** @ ram.lmtas.lmco.com ...
Maybe
return obj.className="biggerlink";
--
George Hester
__________________________________
"Michael Hill" <hi****@ram.lmtas.lmco.com> wrote in message news:40***************@ram.lmtas.lmco.com...
我使用动态创建了一个元素javascript如:
myA = document.createElement(" A");
myA.href =" javascript :acton(this)" ;;
myA.className =" smallLink";
myText = document.createTextNode(''sometext'');
myA.appendChild(myText);
然后当我按下链接我想把这个
链接的''className'从''smallLink''更改为''largeLink'',但没有任何反应。
功能行动(obj)
{alert(obj);
obj.className =" higherlink";
}
当我看到alert(obj)我得到[object]。
当我看到alert(obj.className)时,我得到undefined。
任何人都知道我做错了什么在这里?
Mike
I created an element on-the-fly using javascript like:
myA=document.createElement("A");
myA.href="javascript:acton(this)";
myA.className = "smallLink";
myText=document.createTextNode(''sometext'');
myA.appendChild(myText);
Then when I pressed the link I''d like to change the ''className'' of this
link from ''smallLink'' to ''biggerLink'', but nothing happens.
function action(obj)
{
//alert(obj);
obj.className="biggerlink";
}
When I look at alert(obj) I get "[object]".
When I look at alert(obj.className) I get "undefined".
Anyone know what I am doing wrong here?
Mike
" Michael Hill" <喜**** @ ram.lmtas.lmco.com>在消息中写道
news:40 *************** @ ram.lmtas.lmco.com ...
< snip>
"Michael Hill" <hi****@ram.lmtas.lmco.com> wrote in message
news:40***************@ram.lmtas.lmco.com...
<snip>
myA = document.createElement(" A");
myA.href =" javascript :acton(this)" ;;
myA.className =" smallLink";
myText = document.createTextNode(''sometext'');
myA.appendChild(myText);
< snip>功能动作(obj)
// alert(obj);
obj.className =" greaterlink" ;;
}
myA=document.createElement("A");
myA.href="javascript:acton(this)";
myA.className = "smallLink";
myText=document.createTextNode(''sometext'');
myA.appendChild(myText); <snip> function action(obj)
{
//alert(obj);
obj.className="biggerlink";
}
< snip>
Javascript伪协议HREF不是创建对象的方法
并且在全局范围内执行,所以 - 这 - 指的是全局
对象和在全局
对象上设置/创建 - className - 属性是没有意义的。
你永远不应该使用javascript伪协议HREF: -
< URL: http://jibbering.com/faq/#FAQ4_24 >
- 但要解决您的具体问题,您需要指定一个onclick
$改为创建对象的b $ b方法(虽然你需要链接到
有一个HREF,否则它不会是一个链接,但它可能是一个
代替SPAN元素)。例如: -
myA.onclick = function(){
this.className =" higherlink" ;;
返回false ;
}
- 或 -
myA.onclick = new Funciton(" this.className = \更大链接\;返回
false;");
- 取决于您是否理解分配
事件处理程序的内部函数与否(和/或ECMA
脚本压缩配置文件是否相关)。
Richard 。
<snip>
Javascript pseudo protocol HREFs are not methods of the created object
and are executed in the global scope, so - this - refers to the global
object and setting/creating a - className - property on the global
object is pointless.
You should never use javascript pseudo protocol HREFs:-
<URL: http://jibbering.com/faq/#FAQ4_24 >
- but to solve your specific problem you need to assign an onclick
method to the created object instead (though you will need the link to
have a HREF else it will not be a link, but then it probably could be a
SPAN element instead). E.g.:-
myA.onclick = function(){
this.className="biggerlink";
return false;
}
- or -
myA.onclick = new Funciton("this.className=\"biggerlink\";return
false;");
- depending on whether you understand the consequences of assigning an
inner function to an event handler or not (and/or whether the ECMA
Script compact profile will be relevant).
Richard.
>
- 但要解决您的具体问题,您需要为创建的对象分配onclick
方法(虽然你需要链接到
有一个HREF,否则它不会是一个链接,但它可能是一个
SPAN元素而不是)。例如: -
myA.onclick = function(){
this.className =" higherlink" ;;
return false;
}
- but to solve your specific problem you need to assign an onclick
method to the created object instead (though you will need the link to
have a HREF else it will not be a link, but then it probably could be a
SPAN element instead). E.g.:-
myA.onclick = function(){
this.className="biggerlink";
return false;
}
理查德,
您是否建议我更改为:
mySPAN = document.createElement (" SPAN");
mySPAN.className =" smallLink";
mySPAN.onclick = new Function(" this.className = \" higherlink) \" ;;返回
false;");
myText = document.createTextNode(''sometext'');
mySPAN .appendChild(myText);
Mike
Richard,
Are you suggesting I change to something like:
mySPAN=document.createElement("SPAN");
mySPAN.className = "smallLink";
mySPAN.onclick = new Function("this.className=\"biggerlink\";return
false;");
myText=document.createTextNode(''sometext'');
mySPAN.appendChild(myText);
Mike
这篇关于使用动态创建的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!