Jquery,实时"title"属性变化
Jquery, real time "title" attribute change
我有一个跨度,每秒在工具提示中显示来自服务器的一些数据:
<span class="bata hastip" title="{will change}"></span>
我正在使用 tooltipsy 插件来显示工具提示:
$('.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
});
"You are referring to DOM Mutation Events. There is poor (but improving) browser support for these events. Mutation Events plugin for jQuery might get you some of the way."
查看相关问题:firing event on DOM attribute change
快捷方式 - 检查这个:)
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();
我有一个跨度,每秒在工具提示中显示来自服务器的一些数据:
<span class="bata hastip" title="{will change}"></span>
我正在使用 tooltipsy 插件来显示工具提示:
$('.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
});
"You are referring to DOM Mutation Events. There is poor (but improving) browser support for these events. Mutation Events plugin for jQuery might get you some of the way."
查看相关问题:firing event on DOM attribute change
快捷方式 - 检查这个:)
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();