Google places autocomplete, how to clean up pac-container?

后端 未结 5 1975
鱼传尺愫
鱼传尺愫 2021-02-12 13:41

I\'m using the google places autocomplete control, and it creates an element for the drop down with a class pac-container.

I\'m using the autocomplete in a

5条回答
  •  無奈伤痛
    2021-02-12 13:55

    Built this recursive function to locate element position inside autocomplete object.


    Get first matching object

    var elementLocator = function(prop, className, maxSearchLevel, level) {
    
        level++;
    
        if (level === (maxSearchLevel + 1) || !prop || !(Array.isArray(prop) || prop === Object(prop))) {
            return;
        }
    
        if (prop === Object(prop) && prop.classList && prop.classList.contains && typeof prop.classList.contains === 'function' && prop.classList.contains(className)) {
            return prop;
        }
    
        for (const key in prop) {
            if (prop.hasOwnProperty(key)) {
                var element = elementLocator(prop[key], className, maxSearchLevel, level);
                if (element) {
                    return element;
                }
            }
        }
    };
    

    Usage:

    var elm = null;
    try {
        //set to search first 12 levels, pass -1 to search all levels
        elm = elementLocator(this.autocomplete, 'pac-container', 12, null); 
    } catch(e) {
        console.log(e);
    }
    

提交回复
热议问题