代码之家  ›  专栏  ›  技术社区  ›  Gammer

javascript:tippy.js不适用于动态内容

  •  2
  • Gammer  · 技术社区  · 6 年前

    我有以下提示:Ajax调用将在何处悬停,并获取数据,创建内容并显示出来。但它不适用于动态内容,因为在页面上加载

    <span class="more-tags otherPostTags" data-postId="{{$post->id}}">...</span>

    在页面上是静态的,但在选项卡中也是动态的。

    所以下面的代码适用于静态

    <span class=“more tags otherPosttags”data post id=“$post->id”>…</span>

    但不适用于动态。

    <div id="template" style="display: none;">
        Loading a new image...
    </div>
    
    <span class="more-tags otherPostTags" data-postId="{{$post->id}}">...</span>
    

    Tippy jQuery:

    const template = document.querySelector('#template');
    const initialText = template.textContent;
    
    const tip = tippy('.otherPostTags', {
        animation: 'shift-toward',
        arrow: true,
        html: '#template',
        onShow() {
            const content = this.querySelector('.tippy-content')
            if (tip.loading || content.innerHTML !== initialText) return
            tip.loading = true
            node = document.querySelectorAll('[data-tippy]');
            let id = node[0].dataset.postid;
            $.ajax({
                url: '/get/post/'+id+'/tags',
                type: 'GET',
                success: function(res){
                    let preparedMarkup = '';
                    res.tags.map(function(item) {
                        preparedMarkup +=
                            '<span class="orange-tag" style="background-color: '+item.color+'">'+
                                item.name +
                            '</span>';
                    });
                    content.innerHTML = preparedMarkup;
                    tip.loading = false
                },
                error: function(error) {
                    console.log(error);
                    content.innerHTML = 'Loading failed';
                    tip.loading = false
                },
            });
        },
        onHidden() {
            const content = this.querySelector('.tippy-content');
            content.innerHTML = initialText;
    
        },
        popperOptions: {
            modifiers: {
                preventOverflow: {
                    enabled: false
                },
                hide: {
                    enabled: false
                }
            }
        }
    });
    

    我这里缺什么?

    1 回复  |  直到 6 年前
        1
  •  1
  •   T.J. Crowder    6 年前

    如果希望Tippy在新元素上激活,则需要使用事件委托。这个 Tippy documentation 包括这一点(令人沮丧的是,没有锚链接;搜索“事件委托”)。使用父容器元素,然后告诉Tippy使用什么选择器来匹配子元素。文档中的示例是:

    tippy('#parent', {
      target: '.child'
    })
    

    …因此,对于您的示例,使用容器 .otherPostTags 元素在中( document.body 在最坏的情况下)和使用 其他邮戳 作为 target :

    tippy('selectorForParentElement', {
      target: '.otherPostTags'
    });
    

    活生生的例子:

    tippy('#container', {
      target: '.otherPostTags'
    });
    var counter = 0;
    var timer = setInterval(function() {
      ++counter;
      var tag = document.createElement("span");
      tag.title = "Tip for span #" + counter;
      tag.className = "otherPostTags";
      tag.innerHTML = "Span #" + counter;
      document.getElementById("container").appendChild(tag);
      if (counter === 6) {
        clearInterval(timer);
      }
    }, 250);
    .otherPostTags {
      color: white;
      background-color: #2020FF;
      border: 1px solid black;
      padding: 2px;
      margin-left: 2px;
      margin-right: 2px;
      border-radius: 4px;
    }
    <link href="https://cdnjs.cloudflare.com/ajax/libs/tippy.js/2.5.4/tippy.css" rel="stylesheet"/>
    
    <div id="container"></div>
    
    <script src="https://cdnjs.cloudflare.com/ajax/libs/tippy.js/2.5.4/tippy.min.js"></script>