レガシーアプリケーションのhtml構造を変更したくありません。いくつかのIDのツールチップを追加するには、JavaScriptを上に追加するだけです。
いくつかのライブラリは、特定のHTML構造に依存するオプション、またはレガシーアプリケーションでは困難な完全なツールチップライブラリの統合方法を示しています。
このようなものですが、HTMLを追加していません。IDを見つけて、単純な黒い背景のツールチップに必要なものを適用するだけです。StackOverflowレピュテーションページのツールチップのように。
window.addEventListener("load", function () {
var couponcodes = document.getElementsByClassName("couponcode");
for (var i = 0; i < couponcodes.length; i++) {
couponcodes[i].addEventListener("mouseover", function () {
var coupontooltip = this.getElementsByClassName("coupontooltip")[0];
coupontooltip.removeAttribute("style");
});
couponcodes[i].addEventListener("mouseout", function () {
var coupontooltip = this.getElementsByClassName("coupontooltip")[0];
coupontooltip.style.display = "none";
});
}
});
http://jsfiddle.net/mynetx/5qbP3/
JS / jQueryのみでIDのツールチップを追加するにはどうすればよいですか?
html構造を変更せずに、JS / jQueryを使用してIDのツールチップを追加します。
jQuery(function($){
//On hover on id create tooltip element and append to that div
$('#add_tooltip').hover(
function () {
$(this).append('<div class = "coupontooltip">Lorem Ipsum is simply dummy text of the printing</div>');
//display the tooltip with animation.
$(this).find('.coupontooltip').hide().fadeIn('slow');
/*if in code already tooltip section is there
$(this).find('.coupontooltip').show();
*/
},
//On hover out remove the tooltip.
function () {
$(this).find('.coupontooltip').remove();
/*if in code already tooltip section is there
$(this).find('.coupontooltip').hide();
*/
}
);
})
#add_tooltip{
cursor: pointer;
}
.coupontooltip{
display: inherit;
background: black;
width: 19px;
/* background: #C8C8C8; */
margin-left: 83px;
padding: 10px;
position: absolute;
z-index: 1000;
width: 200px;
height: 59px;
color: #fff;
top: 12px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
<div id="add_tooltip">Add Tooltip</div>
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加