How to do JavaScript object introspection?

前端 未结 3 1948
春和景丽
春和景丽 2020-12-14 10:51

What to do when after all probing, a reportedly valid object return \'undefined\' for any attribute probed? I use jQuery, $(\'selector\').mouseover(function() { });

相关标签:
3条回答
  • 2020-12-14 11:29

    Is selector the name of the element? If so then you should reference it as:

    $('area#selector')
    

    or

    $('#selector')
    

    otherwise it will attempt to look for the (non-existent) "selector" HTML tag and, obviously, not find it.

    0 讨论(0)
  • 2020-12-14 11:35

    Though this answer is a bit late, I'd still recommend checking out these links:

    http://www.webweavertech.com/ovidiu/weblog/archives/000317.html
    http://www.syger.it/Tutorials/JavaScriptIntrospector.html

    0 讨论(0)
  • 2020-12-14 11:38

    Your question is a bit vague, so maybe you can provide more details?

    As for finding out about an object and the values of its properties, there are many ways to do it, including using Firebug or some other debug tools, etc. Here is a quick and dirty function that might help get you started until you can provide more details:

    function listProperties(obj) {
       var propList = "";
       for(var propName in obj) {
          if(typeof(obj[propName]) != "undefined") {
             propList += (propName + ", ");
          }
       }
       alert(propList);
    }
    

    That will display a list of the properties of the object that you pass it that are not undefined.

    Hope that helps...

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