简体   繁体   English

加载新的html内容工具提示后不起作用

[英]after loading new html content tooltip does not work

I have a problem with displaying a tooltip after loading new elements in a div. 在div中加载新元素后,显示工具提示时出现问题。

My content loading code: 我的内容加载代码:

$ (Function () {
    
     $ ( '. Prop-next'). Click (function () {
         $ ('.props') .html ('<img src = "/ images / loading.gif" class = "loading" />')
         var p = parseInt ($ (this) .attr ('p')) + 3;
         $ (this) .attr ('p', p);
         $ .post ('/ lib / props.php', {p: p}, function (item) {
             $ ( '. Props'). Html (item);
         });
     });
});

when calling the .prop-next click element in the div .props new content appears, while the script does not work: 当在div .props中调用.prop-next click元素时,将显示新内容,而脚本不起作用:

tooltip code: 工具提示代码:

var tooltips = document.querySelectorAll ('.tooltip');

window.onmousemove = function (e) {
     var x = (e.clientX - 350) + 'px',
         y = (e.clientY + 50) + 'px';
     for (var i = 0; i <tooltips.length; i ++) {
         tooltips [i] .style.top = y;
         tooltips [i] .style.left = x;
     }
};

your code that binds mouse movement is done only once,, on page load, and that for consequence has that new items (the one that comes down the wire via AJAX) are not having that binging added.. instead of: 您的绑定鼠标移动的代码仅在页面加载时执行一次,其结果是新项(通过AJAX从网络上下来的项)没有添加该绑定。.而不是:

window.onmousemove = function (e) { // .. }

try so add dinamicly, or live hooking,, something like this should work: 尝试这样动态添加或实时挂接,这样的方法应该起作用:

$('body').on('mousemove', '.tooltip', function(e) {
  var x = (e.clientX - 350) + 'px',
    y = (e.clientY + 50) + 'px';
  for (var i = 0; i < tooltips.length; i++) {
    tooltips[i].style.top = y;
    tooltips[i].style.left = x;
  }
});

in this line: $('body').on('mousemove', '.tooltip', .. you define that your parent element (body) track mousemove only on .tooltip element, and that will track your new element that appear on page latter :) 在此行中: $('body').on('mousemove', '.tooltip', ..定义您的父元素(body)仅在.tooltip元素上跟踪mousemove,并且将跟踪出现的新元素在后面的页面上:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM