Jquery,实时“title”属性更改

Jquery, real time "title" attribute change

本文关键字:属性 title 实时 Jquery      更新时间:2023-09-26

我有一个跨度,每秒在工具提示中显示来自服务器的一些数据:

<span class="bata hastip" title="{will change}"></span>

我正在使用工具提示插件来显示工具提示:

$('.hastip').tooltipsy({
        offset: [-10, 0],
        css: {
            'background-color': '#444F54',
            'border': '1px solid #888'
        }
})

由于我的数据每隔几秒钟就会发生变化,我如何检测这种变化?像这样:

$('.bata').prop('title', datas);
//$('.bata').attr('title', datas) same...
$(document).on('change', '.bata', function(){
   //this method doesn't work
});

"你指的是 DOM 突变事件。浏览器对这些事件的支持很差(但正在改进)。jQuery的突变事件插件可能会给你一些方法。

请参阅此相关问题:DOM 属性更改时的触发事件

快捷方式 - 检查此:)

https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver

支持相当不错,除了IE11+

MDN 的示例用法:

// select the target node
var target = document.querySelector('#some-id');
// create an observer instance
var observer = new MutationObserver(function(mutations) {
  mutations.forEach(function(mutation) {
    console.log(mutation.type);
  });    
});
// configuration of the observer:
var config = { attributes: true, childList: true, characterData: true };
// pass in the target node, as well as the observer options
observer.observe(target, config);
// later, you can stop observing
observer.disconnect();