twitter-bootstrap 无法在初始化之前调用工具提示上的方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19970082/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot call methods on tooltip prior to initialization
提问by MD. ABDUL Halim
I am using jQuery tooltip for dynamic created row(can be 10 row/more row)
我正在使用 jQuery 工具提示动态创建行(可以是 10 行/更多行)
Tooltip is being display properly but close is not being properly.
工具提示正确显示,但关闭不正确。
Error is given below,
下面给出错误,
Error: cannot call methods on tooltip prior to initialization; attempted to call method 'close'
throw new Error( msg );
while(m < 10){
.......................................
.......................................
if(data =="EXIST")
{
display_tp_tooltip(m);
$("#tp_no"+m).val("");
}
else
{
display_tp_tooltip_close(m);
}
}
function display_tp_tooltip(m)
{
$("#tp_no"+m).tooltip();
$("#tp_no"+m).tooltip({
open: function (e, o) {
o.tooltip.animate({ top: o.tooltip.position().top + 10 }, "fast" );
$(o.tooltip).mouseover(function (e) {
$("#tp_no"+m).tooltip('close');
});
$(o.tooltip).mouseout(function (e) {});
},
position: {
my: "center bottom-20",
at: "center top",
using: function( position, feedback ) {
$( this ).css( position );
$( "<div>" )
.addClass( "arrow" )
.addClass( feedback.vertical )
.addClass( feedback.horizontal )
.append($('<style>.ui-tooltip,.arrow:after { background:red; }</style>'))
.appendTo( this );
}
},
content: function() { return cellTpNoTooltipContent()},
close: function (e, o) {},
show: {
duration: 800
}
});
$("#tp_no"+m).tooltip('open');
setTimeout(function () {
$(this).tooltip('close'); //close the tooltip
}, 3000);
}
function cellTpNoTooltipContent()
{
var rval = "T.P No. is exist";
return rval;
}
function display_tp_tooltip_close(m)
{
$("#tp_no"+m).tooltip("close");
}
How can i solve it? Please help me.
我该如何解决?请帮我。
回答by omarjebari
I found that declaring bootstrap.js before jquery-ui.js in my caused the problem. Make sure that jquery-ui.js is declared before bootstrap.js.
我发现在 jquery-ui.js 之前声明 bootstrap.js 导致了问题。确保在 bootstrap.js 之前声明了 jquery-ui.js。
回答by George Henrique
Declare jquery-ui.js before the bootstrap.js
在 bootstrap.js 之前声明 jquery-ui.js
回答by Brett
You can't call a method from the tooltip widget (e.g .tooltip('close')) before the widget has been initialized, either with a call to .tooltip()or .tooltip({ })
.tooltip('close')在小部件初始化之前,您不能从工具提示小部件(例如)调用方法,无论是调用.tooltip()还是.tooltip({ })
You can prevent this error by testing that the DOM element is an instance of the tooltip widget:
您可以通过测试 DOM 元素是否是工具提示小部件的实例来防止此错误:
if($("#tp_no"+m).is(':ui-tooltip')){
$("#tp_no"+m).tooltip("close");
}
Another option would be to move the call to $("#tp_no"+m).tooltip();as the first thing inside your while(m < 10)loop to ensure that whichever branch your if statement takes, the element will be an instance of the tooltip widget
另一种选择是将调用移动到循环中$("#tp_no"+m).tooltip();的第一件事,while(m < 10)以确保无论您的 if 语句采用哪个分支,该元素都将是工具提示小部件的实例
回答by reformed
You'll need to prevent the jQuery UI library from overwriting jQuery.fn.tooltip.
您需要防止 jQuery UI 库覆盖jQuery.fn.tooltip.
So cache this value before loading the jQuery UI library, then restore it afterward:
所以在加载 jQuery UI 库之前缓存这个值,然后再恢复它:
<script>
let _tooltip = jQuery.fn.tooltip; // <--- Cache this
</script>
<script src="/path/to/jquery-ui.min.js"></script> <!-- load jQuery UI -->
<script>
jQuery.fn.tooltip = _tooltip; // <--- Then restore it here
</script>
回答by Mike
Declaring jquery-ui.js before the bootstrap.js also solved my issue of
在 bootstrap.js 之前声明 jquery-ui.js 也解决了我的问题
TypeError: elem.getClientRects is not a function at jQuery.fn.init.offset
类型错误:elem.getClientRects 不是 jQuery.fn.init.offset 的函数
回答by Judson Terrell
I always check to see if the elements exists before calling close methods etc.
我总是在调用 close 方法等之前检查元素是否存在。
If($(myelem).length > -1) do something
If($(myelem).length > -1) 做点什么

