...

View Full Version : how to add function to the dom object?



renzaijiang
04-17-2009, 03:26 PM
i'm from china,maybe my english is poor but i think you can understand my question.
i wrote the code below:


<div>hello </div>
<div>i'm jacky</div>
<script>
document.getElementsByTagName("div").each=function(fn)
{
if(typeof(fn)=="function")
{
for(e=new Enumerator(this);!e.atEnd();e.moveNext())
{
fn(e.item());
}
}
}
document.getElementsByTagName("div").each(function(e){alert(e.innerText)});
</script>

but the function "each" can just have effects on tag "div" i want to all the html tags can use function "each" what can i do ,i found the dom object does have the attribute prototype .

oesxyl
04-17-2009, 03:47 PM
i'm from china,maybe my english is poor but i think you can understand my question.
i wrote the code below:



<div>hello </div>
<div>i'm jacky</div>
<script>
document.getElementsByTagName("div").each=function(fn)
{
if(typeof(fn)=="function")
{
for(e=new Enumerator(this);!e.atEnd();e.moveNext())
{
fn(e.item());
}
}
}
document.getElementsByTagName("div").each(function(e){alert(e.innerText)});
</script>

but the function "each" can just have effects on tag "div" i want to all the html tags can use function "each" what can i do ,i found the dom object does have the attribute prototype .
not sure what happend, I just say what I think but I could be wrong.
I search for 'each' and I can't find any reference about it.
I do some test and I see that work so I suppose that you define a new function in that line.
Back to your question, if you want to apply this to any html tag why don't you use '*' instead of 'div'? I didn't test this but I have no reason to belive that will not work.

please put your code between [ code] and [ /code] tags. Thank you
best regards

renzaijiang
04-17-2009, 04:11 PM
maybe you don't understands me
i want to make a function just like function each in "jquery" framework,
when i change div tags to other tags
the function(e){alert(e.innerText)} also have effect on
I have discuss the question in csdn.net in china but somebody tell me
dom can't be extend.but how do some framework achieved this job.

oesxyl
04-17-2009, 04:42 PM
maybe you don't understands me
i want to make a function just like function each in "jquery" framework,
when i change div tags to other tags
the function(e){alert(e.innerText)} also have effect on
I have discuss the question in csdn.net in china but somebody tell me
dom can't be extend.but how do some framework achieved this job.
seems that I still don't understand you, :)
what do you mean by "i change div tags to other tags"? to change it in getElementsByTagName or to replace in dom the div node with a node with other tag?
in last case anything is assigned to the div node will be lost and you must assign it again each time you create a new node.

PS: I don't think you ask how to use prototype? I'm wrong?

best regards

renzaijiang
04-18-2009, 11:10 AM
I'm sorry
I means
like the code below


<div>hello </div>
<span>i'm jacky</span> /*i change here ,for example change it to be span*/

<script>
document.getElementsByTagName("div").each=function(fn)
{
if(typeof(fn)=="function")
{
for(e=new Enumerator(this);!e.atEnd();e.moveNext())
{
fn(e.item());
}
}
}
document.getElementsByTagName("div").each(function(e){alert(e.innerText)});
</script>


the result will show alert message "hello "
if i change the red sentence to be "document.getElementsByTagName("span").each(function(e){alert(e.innerText)}); "
I hope it will show alert message "i'm jacky" .
I don't how to change the function to achieve this goal.

oesxyl
04-18-2009, 01:57 PM
I'm sorry
I means
like the code below


<div>hello </div>
<span>i'm jacky</span> /*i change here ,for example change it to be span*/

<script>
document.getElementsByTagName("div").each=function(fn)
{
if(typeof(fn)=="function")
{
for(e=new Enumerator(this);!e.atEnd();e.moveNext())
{
fn(e.item());
}
}
}
document.getElementsByTagName("div").each(function(e){alert(e.innerText)});
</script>


the result will show alert message "hello "
if i change the red sentence to be "document.getElementsByTagName("span").each(function(e){alert(e.innerText)}); "
I hope it will show alert message "i'm jacky" .
I don't how to change the function to achieve this goal.
maybe this is closer to what you want:

http://codingforums.com/showpost.php?p=644446&postcount=4

best regards

renzaijiang
04-19-2009, 10:20 AM
it's great!save the dom object in array and handle it ,I know how to do!
thank oesxyl



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum