简体   繁体   English

Bootstrap工具提示不适用于动态生成的元素

[英]Bootstrap tooltip doesn't work on dynamically generated elements

Code for generating element dynamically: 用于动态生成元素的代码:

 var rStatus = $("#statusText").val(); var roleID = $('#' + Globals.divUserRoleID).html().trim(); var nodelen = response.tsuites.length; $("#TestSuiteSideMenu").empty(); for(var ts=0;ts<nodelen;ts++){ var testSuiteName = response.tsuites[ts]; var testNum = response.tnames[testSuiteName].length; var tsMenu = "<li class='treeview' id='suitemenu'>"+ "<a href='#' ><i class='fa fa-cubes'></i><span value="+testSuiteName+" id=edit-"+testSuiteName+">"+testSuiteName+"</span><i class='fa fa-angle-left pull-right'></i></a>"+"<ul class='treeview-menu' id="+testSuiteName+">"+ "<li class='suite-options'>"+ "<p align='center'>"+ "<button type='button' id='btnSuiteRun-"+testSuiteName+"' class='btn test-btns btn-xs runsuite' data-toggle='tooltip' title='Run Suite'><img src='/assets/images/play_win_icon.svg' class='playwin'></button>"+ "<button type='button' id='btnSuiteEdit-"+testSuiteName+"' class='btn test-btns btn-xs editsuite' data-toggle='tooltip' title='Rename Suite'><i class='fa fa-pencil'></i></button>"+ "<button type='button' id='btnSuiteImport-"+testSuiteName+"' class='btn test-btns btn-xs importTest' name='"+testSuiteName+"' data-toggle='tooltip' title='Import Test' onclick='loadSuites(this.name)'><i class='fa fa-download'></i></button>"+ //"<button type='button' id='btnSuiteLog-"+testSuiteName+"' class='btn test-btns btn-xs suitelog' data-toggle='tooltip' title='Suite Log'><i class='fa fa-file-text'></i></button>"+ "<button type='button' id='btnSuiteClone-"+testSuiteName+"' class='btn test-btns btn-xs clonesuite' data-toggle='tooltip' title='Clone Suite'><i class='fa fa-clone'></i></button>"+ "<button type='button' id='btnSuiteDelete-"+testSuiteName+"' class='btn test-btns btn-xs deletesuite' data-toggle='tooltip' title='Delete Suite'><i class='fa fa-trash'></i></button>"+ "</p>"+"</li>"+"</ul></li>"; } $("#TestSuiteSideMenu").append(tsMenu); $("[data-toggle='tooltip']").tooltip(); 

Following code is generated dynamically: 以下代码是动态生成的:

 <ul class="treeview-menu menu-open" id="htrbg" style="display: block;"> <li class="suite-options"> <p align="center"> <button type="button" id="btnSuiteEdit-htrbg" class="btn test-btns btn-xs editsuite" data-toggle="tooltip" title="Rename Suite"><i class="fa fa-pencil"></i></button> <button type="button" id="btnSuiteImport-htrbg" class="btn test-btns btn-xs importTest" name="htrbg" data-toggle="tooltip" title="Import Test" onclick="loadSuites(this.name)"><i class="fa fa-download"></i></button> <button type="button" id="btnSuiteClone-htrbg" class="btn test-btns btn-xs clonesuite" data-toggle="tooltip" title="Clone Suite"><i class="fa fa-clone"></i></button> <button type="button" id="btnSuiteDelete-htrbg" class="btn test-btns btn-xs deletesuite" data-toggle="tooltip" title="Delete Suite"><i class="fa fa-trash"></i></button> </p> </li> </ul> 

When I hover on the button, it makes the title attribute of the button as blank and adds an attribute aria-describedby with values as 'ui-id-2'. 当我将鼠标悬停在按钮上时,它将按钮的title属性设置为空白,并添加一个属性aria-describedby,其值为'ui-id-2'。 But I cannot see the tooltip. 但是我看不到工具提示。 Can someone please help me? 有人可以帮帮我吗? I have also disabled jquery ui tooltips but it still doesn't work. 我也禁用了jQuery ui工具提示,但仍然无法正常工作。

You need to add .tooltip() to the newly created elements, because they have no action bind on hover event. 您需要将.tooltip()添加到新创建的元素,因为它们没有对悬停事件进行绑定的操作。 The time you use $("[data-toggle='tooltip']").tooltip(); 您使用$("[data-toggle='tooltip']").tooltip(); , it is applied to existing [data-toggle='tooltip'] elements, not the future ones. ,它将应用于现有的[data-toggle='tooltip']元素,而不是将来的元素。

You have to instantiate tooltip() on newly appended elements... 您必须在新添加的元素上实例化tooltip() ...

But only on them! 但是只有他们! It's not a really good idea to re-run the init on already tooltiped ones... 在已经提示的工具上重新运行init不是一个好主意...

So I suggest you use a class like nottooltipped in the append process (particularly if you have many)... which will enable you to lookup these elements and tooltip them. 因此,我建议您在附加过程中使用诸如nottooltipped类的类(尤其是如果您有很多类)……这将使您能够查找这些元素并对其进行提示。

See below: 见下文:

 $(document).ready(function(){ $("[data-toggle='tooltip']").tooltip(); // After 5 seconds... Simulating a dynamic append to add some buttons setTimeout(function(){ $(".suite-options p").append('<button type="button" id="btnSuiteImport-htrbg" class="btn test-btns btn-xs importTest nottooltipped" name="htrbg" data-toggle="tooltip" title="I WAS ADDED !" onclick="loadSuites(this.name)"><i class="fa fa-surprise"></i></button>'); // HERE! lookup the freshly appended elements to tooltip $(".nottooltipped").removeClass("nottooltipped").tooltip(); },5000); }); 
 .suite-options{ list-style:none; } .fa-surprise{ color:red; } 
 <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.7.2/css/all.min.css" rel="stylesheet"/> <link href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.css" rel="stylesheet"/> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.js"></script> <ul class="treeview-menu menu-open" id="htrbg" style="display: block;"> <li class="suite-options"> <p align="center"> <button type="button" id="btnSuiteEdit-htrbg" class="btn test-btns btn-xs editsuite" data-toggle="tooltip" title="Rename Suite"><i class="fa fa-pencil-alt"></i></button> <button type="button" id="btnSuiteImport-htrbg" class="btn test-btns btn-xs importTest" name="htrbg" data-toggle="tooltip" title="Import Test" onclick="loadSuites(this.name)"><i class="fa fa-download"></i></button> <button type="button" id="btnSuiteClone-htrbg" class="btn test-btns btn-xs clonesuite" data-toggle="tooltip" title="Clone Suite"><i class="fa fa-clone"></i></button> <button type="button" id="btnSuiteDelete-htrbg" class="btn test-btns btn-xs deletesuite" data-toggle="tooltip" title="Delete Suite"><i class="fa fa-trash"></i></button> </p> </li> </ul> 

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

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