JavaScript Question dealing with different browsers

后端 未结 5 1459
抹茶落季
抹茶落季 2021-01-27 13:03

The problem that I\'m having is that my code works fine in JavaScript but doesn\'t work correctly in Firefox or safari and wondering why. What I\'m doing is I have a loop going

相关标签:
5条回答
  • 2021-01-27 13:16

    Try using document.getElementsByTagName, as I believe this is supported across all browsers.

    var linkList = document.getElementsByTagName("a");
    
    0 讨论(0)
  • 2021-01-27 13:19

    http://jsfiddle.net/aaronfrost/Xra5y/1/ That has your answer.

    0 讨论(0)
  • 2021-01-27 13:22

    I don't know asp tags. I assume req is a attribute so this should do what you asked by only alerting if the attributes are equal to your requirments, I use a shortcut array to hold all the found elements by tag name as James said its more crossbrowser.

    var a = [];
    a = document.getElementsByTagName("input");
    for(var i=0; i < a.length; i++){
    
    if (theForm.elements[i].getAttribute('type')== "text" &&     theForm.elements[i].getAttribute('req') == "yes" ){
    alert("Made it here wtih" + theForm.elements[i]) 
    }
    } 
    
    0 讨论(0)
  • 2021-01-27 13:26

    You should use var theForm = getElementById("theForm") instead of calling theForm directly. Only Chrome and IE adds the elements ids to the javascript global scope.

    And you should use getAttribute() to get the value of an attribute because only IE has this shortcut.

    Try this example.

    0 讨论(0)
  • 2021-01-27 13:27

    Use getAttribute to read the custom attributes. See http://jsfiddle.net/8EWQr/.

    So instead of

    (theForm.elements[a].type == "text" && theForm.elements[a].req == "yes")

    use

    (theForm.elements[a].getAttribute('type') == "text" && theForm.elements[a].getAttribute('req') == "yes")

    0 讨论(0)
提交回复
热议问题