Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Apr 2009
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    how to add function to the dom object?

    i'm from china,maybe my english is poor but i think you can understand my question.
    i wrote the code below:

    Code:
    <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 .
    Last edited by renzaijiang; 04-17-2009 at 02:57 PM.

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by renzaijiang View Post
    i'm from china,maybe my english is poor but i think you can understand my question.
    i wrote the code below:

    Code:
    <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.

    Edit: please put your code between [ code] and [ /code] tags. Thank you

    best regards
    Last edited by oesxyl; 04-17-2009 at 02:50 PM.

  • #3
    New to the CF scene
    Join Date
    Apr 2009
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #4
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by renzaijiang View Post
    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

  • #5
    New to the CF scene
    Join Date
    Apr 2009
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I'm sorry
    I means
    like the code below
    Code:
    <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.

  • #6
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by renzaijiang View Post
    I'm sorry
    I means
    like the code below
    Code:
    <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:

    insert loop into function to handle several forms

    best regards

  • #7
    New to the CF scene
    Join Date
    Apr 2009
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    it's great!save the dom object in array and handle it ,I know how to do!
    thank oesxyl


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •