Javascript Can';无法通过onclick函数解决关闭工具提示的问题

Javascript Can';无法通过onclick函数解决关闭工具提示的问题,javascript,tooltip,Javascript,Tooltip,我正在尝试创建一个函数,该函数将通过“单击”父元素来关闭我的工具提示,该父元素通过第一次单击返回该工具提示。需要专门为工具提示父元素获取关闭函数。现在一切正常,但我也可以通过单击body元素上的任意位置来关闭工具提示 `(function () { function Tooltip(options) { if (!options) options = {}; var self = this; this.tooltips; t

我正在尝试创建一个函数,该函数将通过“单击”父元素来关闭我的工具提示,该父元素通过第一次单击返回该工具提示。需要专门为工具提示父元素获取关闭函数。现在一切正常,但我也可以通过单击body元素上的任意位置来关闭工具提示

`(function () {
    function Tooltip(options) {
        if (!options) options = {};
        var self = this;
        this.tooltips;
        this.offset = 5;
        this.beforeTooltip = options.beforeTooltip;
        this.afterTooltip = options.afterTooltip;
        this.tooltipWrapper = document.createElement('div');
        this.status = false;
        this.tooltip = function (elem) {
            if (!elem.classList.contains('active')){
                if (this.status) this.remElemActive();
                if (this.beforeTooltip) this.beforeTooltip(elem);
                elem.classList.add('active');
                var coords = this.getCoords(elem);
                this.tooltipWrapper.textContent = elem.dataset.tooltip;
                this.tooltipWrapper.classList.add('active');
                this.tooltipWrapper.style.top = coords.top - (this.tooltipWrapper.offsetHeight + this.offset) + 'px';
                this.tooltipWrapper.style.left = (coords.left + coords.width / 2) - (this.tooltipWrapper.offsetWidth / 2) + 'px';
                this.status = true;
                if (this.status){
                    setTimeout(function () {
                        document.addEventListener('click', self.closeTipsBody, false);
                    }, 100)
                }
                if (this.afterTooltip) this.afterTooltip(elem)
            }else {
                elem.classList.remove('active');
            }
        };
        this.closeTipsBody = function (e) {
            if (self.tooltipWrapper === e.target || e.target.classList.contains('active')){
                return false
            }
            self.closeTips();
        };
        this.closeTips = function () {
            this.tooltipWrapper.classList.remove('active');
            this.remElemActive();
            this.status = false;
            document.removeEventListener('click', self.closeTipsBody, false)
        };
        this.remElemActive = function () {
            document.querySelector('.tooltip-js').classList.remove('active')
        };
        this.getCoords = function (elem) {
            elem = elem.getBoundingClientRect();
            return{
                top: elem.top + window.pageYOffset,
                left: elem.left + window.pageXOffset,
                width: elem.width
            }
        };
        this.init = function () {
            document.addEventListener('DOMContentLoaded', function () {
                this.tooltips = document.querySelectorAll('.tooltip-js');
                this.tooltipWrapper.classList.add('tooltip-box');
                document.querySelector('body').appendChild(this.tooltipWrapper);
                for (var i = 0; i < this.tooltips.length; i++ ){
                    this.tooltips[i].addEventListener('click', function (e) {
                        e.preventDefault();
                        self.tooltip(this);
                    })
                }
            }.bind(this))
        };
        this.init();
    }
    window.Tooltip = Tooltip;
})();`
`(函数(){
功能工具提示(选项){
如果(!options)options={};
var self=这个;
这是工具提示;
这个偏移量=5;
this.beforeTooltip=options.beforeTooltip;
this.afterTooltip=options.afterTooltip;
this.tooltipWrapper=document.createElement('div');
这个.status=false;
this.tooltip=函数(elem){
如果(!elem.classList.contains('active')){
如果(this.status)this.remElemActive();
if(this.beforeTooltip)this.beforeTooltip(elem);
元素classList.add('active');
var coords=this.getCoords(elem);
this.tooltipWrapper.textContent=elem.dataset.tooltip;
this.tooltipWrapper.classList.add('active');
this.tooltipWrapper.style.top=coords.top-(this.tooltipWrapper.offsetHeight+this.offset)+'px';
this.tooltipWrapper.style.left=(coords.left+coords.width/2)-(this.tooltipWrapper.offsetWidth/2)+'px';
this.status=true;
如果(此状态){
setTimeout(函数(){
document.addEventListener('click',self.closeTipsBody,false);
}, 100)
}
如果(此后ToolTip)此后ToolTip(elem)
}否则{
elem.classList.remove('active');
}
};
this.closeTipsBody=函数(e){
if(self.tooltipWrapper==e.target | | e.target.classList.contains('active')){
返回错误
}
self.closeTips();
};
this.closeTips=函数(){
this.tooltipWrapper.classList.remove('active');
this.remElemActive();
这个.status=false;
document.removeEventListener('click',self.closeTipsBody,false)
};
this.remElemActive=函数(){
document.querySelector('.tooltip js').classList.remove('active'))
};
this.getCoords=函数(elem){
elem=elem.getBoundingClientRect();
返回{
顶部:elem.top+window.pageYOffset,
左:elem.left+window.pageXOffset,
宽度:元素宽度
}
};
this.init=函数(){
document.addEventListener('DOMContentLoaded',函数(){
this.tooltips=document.querySelectorAll('.tooltipjs');
this.tooltipWrapper.classList.add('tooltip-box');
document.querySelector('body').appendChild(this.tooltipWrapper);
对于(var i=0;i
如果你需要任何其他信息,关于我想得到什么,发短信给我